i am trying to create a node script, which takes a xlsx file and extracts the data from it using convert-excel-to-json and then creates a migrate-mongo script, which will insert the data in mongo collection.
i was able to make the node script, but there is a date field in my collection. in the xlsx sheet the data is in dd/mm/yyyy format. i extracted the dates and in node script i tried to map it like
"startDate": new Date("yyyy-mm-ddTHH:MM:ssZ");
and then inserted it into file to create the mongo script dataset using
fs.writeFileSync("dataToInsert.js", JSON.stringify(newData, null, 2), 'utf-8');
but the migration file which was created had the date as executed result. i.e.
"startDate": "yyyy-mm-ddTHH:MM:ssZ"
what should be done to get the data in mongo-migrate script as new Date("yyyy-mm-ddTHH:MM:ssZ"); so that this will execute while inserting data into the mongoDb and create proper date field and not string field.
EDIT: as mentioned in comments i tried
"startDate" : {"$date": "yyyy-mm-ddTHH:MM:ssZ"}
but this works in newer node verion (tried in node 14). but this date data is neither inserted nor read, when using node v10.16.3.
I think the only way to accomplish what you are trying to do is by the use of the mongo-extended-json https://docs.mongodb.com/manual/reference/mongodb-extended-json/ which uses the format
"startDate" : {"$date": "yyyy-mm-ddTHH:MM:ssZ"}
It looks like the problem lies in the parsing of the data using JSON.stringify.
I would suggest using EJSON from the BSON package to parse the data.
instead of
fs.writeFileSync("dataToInsert.js", JSON.stringify(newData, null, 2), 'utf-8');
try
const { EJSON } = require('bson');
fs.writeFileSync("dataToInsert.js", EJSON.parse(newData)); //utf-8 is default option here