MongoDB: importing dev data

My data exists in a file as a giant JSON array.
[ {DOC1},{DOC2},{DOC3} ]
I’m changing parameters and data as I develop so needed a way to import data that would also drop whatever data was in the collection already. And wa la:


mongoimport --jsonArray --collection [COLLECTION] --file [DATAFILE.JSON] --db [DBNAME] --drop

Understanding how to import MongoDB Date type data via json was hard to find too.
Easiest way to learn is to create a document in mongoDB with a Date type. Export it to a json file and then look at the result.
Commands:
mongoexport –db [DATABASE] –collection [COLLECTION] –out [FILENAME]

{
“name”: “test name”,
“birthdate”: { “$date” : -1051635600000 }
}

So if you creating a json file importing to MongoDB and want to make it a date type you have to make your document look the same. Took me awhile to figure that out. I’ve never used the date type before actually. If you migrating data from MySQL to MongoDB for example to convert MySQL date type to MongoDB via a PHP script you need to use

$date = array();
$temp = new MongoDate(strtotime([MYSQLDATEVALUE]));
$date['$date'] = $temp->sec * 1000;

*not sure why I have to multiple by 1000 but that is what i noticed when comparing dates from mongo and the strtotime(). Difference of 3 zeros. Probably doing something wrong but given up why.
that will return the secs time.

References:

Advertisements
This entry was posted in MongoDB and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s