MongoDb Date Format

I did a bulk insert in MongoDB using NodeJs (native-driver). I have a date field in the data. Anyway, to save the date field as Date , not String ?

I have a date in the format dd/mm/yyyy . In the current scenario, I get the result by repeating voluminous data that converts the date to the format mm/dd/yyyy , then creates a new date and saves it.

Since iteration takes too much time as the amount of data increases; is there any other way to do this?

+4
source share
1 answer

There is no way to tell mongodb (mongoimport) to convert the string "dd / mm / yyyy" to the date type $ during import.

What you can do is change the type after bulk insert. You can run this code in the mongodb (mongo) shell:

  db.your-collection-name.find().forEach(function(element){ var parts = element.date.split("/"); element.date = new Date(parts[2], parts[1], parts[0]); db.your-collection-name.save(element); }); 
+1
source

Source: https://habr.com/ru/post/1487214/


All Articles