Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Date-datatype using mongoimport

I have many GB of data stored in PostgreSQL database and i need those to be imported into the MongoDB. I did this using CSV export and mongoimport.

There are columns like this '2011-06-25' in that CSV and it has been imported as string, not as MongoDate, so i cannot effectively search by date.

I've found this : http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-Example%3AImportingInterestingTypes but the example says, i need to use JSON structure for the file. Do i really need to export JSON file from PostgreSQL?

If i do - how?

If i don't, how to export "MongoDate" through CSV?

like image 788
Radek Simko Avatar asked Jun 25 '11 05:06

Radek Simko


People also ask

Is date a data type in MongoDB?

The recommended way to store dates in MongoDB is to use the BSON Date data type. The BSON Specification refers to the Date type as the UTC datetime and is a 64-bit integer. It represents the number of milliseconds since the Unix epoch, which was 00:00:00 UTC on 1 January 1970.

How do I import a CSV file into Mongoimport?

If you have CSV files (or TSV files - they're conceptually the same) to import, use the --type=csv or --type=tsv option to tell mongoimport what format to expect. Also important is to know whether your CSV file has a header row - where the first line doesn't contain data - instead it contains the name for each column.

Can I import data into MongoDB?

MongoDB Compass can import data into a collection from either a JSON or CSV file.


2 Answers

Actually the first option is pretty fast even with huge data. Here is an example query using the mongo console:

/usr/bin/mongo yourdbname --eval "db.yourcollectionname.find().forEach(function(doc){doc.yourdatefield = new ISODate(doc.yourdatefield);db.yourcollectionname.save(doc)});"
like image 185
webDEVILopers Avatar answered Nov 15 '22 20:11

webDEVILopers


Your options:

  • import the stuff as CSV and convert the data after the import to Date() (either using the mongo console or using a script written in your favorite language)

  • import your data as JSON and using the $date descriptor for converting date strings into Date instances

like image 21
Andreas Jung Avatar answered Nov 15 '22 18:11

Andreas Jung