Basically you have three options, as if you could import CSV directly using mongoimport, it has no idea how to convert dates from this format.
Convert CSV input to JSON format by any means. For your date values, you can use the extended JSON syntax that the tool will recognize. The resulting JSON that you produce can then be passed to mongoimport.
Write your own data import program by reading your CSV input and making the right conversions.
Import the CSV content as is, and then process the data directly in your MongoDB collection using your choice language.
One of three options is to copy the results and update the dates accordingly:
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; db.collection.find({ },{ "Date": 1 }).forEach(function(doc){ var splitDate = doc.Date.split("-"); var mval = months.indexOf( splitDate[1] ); mval = ( mval < 10 ) ? "0" + mval : mval var newDate = new Date( splitDate[2] + "-" + mval + "-" + splitDate[0] ); db.collection.update( { _id: doc._id }, { "$set": { "Date": newDate } } ); })
And this will make sure that your dates are then converted to the correct BSON date format with the same corresponding date values ββthat you expect.
Beware of βlocalβ time zone conversions, you will want to store them as UTC time.
source share