Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
insert an executable code in js file using another code in node 10

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!