Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda