I have a JSON file with some data and want to add date information and then insert the content of this JSON file in mongoDB. The JSON file looks something like this:
[
{
"installationTime": "How do I insert a date Object here?",
"someOtherValues": 0,
...
},
...
]
And the insertion in node.js:
const content = await readFileAsync('pathToJSONfile.json');
// readFileAsync is fs.readFile wrapped in a promise
await db.createCollection('testCollection');
await db.collection('testCollection').insert(JSON.parse(content));
My question is: How do I insert a date Object like ISODate("2016-12-18T18:30:00Z") into the JSON file? Her in this forum I found:
"installationTime": { "$date": "2016-12-18T18:30:00Z" }
But then I get an Error from mongoDB key $date must not start with '$'.
Is my approach even possible with a json file and the insert command from the node driver?
.parse()method which would accept dates formatted like that. Alternately you could usemongoimportinstead, which supports that format. Or just use a plain ISOString, and either replace ( "parse" ) those strings asDateobjects yourself, or even update enmasse once present in the collection.