Friday, 26 May 2017

Insert a date Object from json file in mongodb

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?



via Jochen Kunze

No comments:

Post a Comment