Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda