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

253
Visualizações
MongoDB Date.now() format different from new Date(string) format

I'm struggling to understand the logic behind MongoDB date formatting, if I save a field like so:

date: Date.now()

in the database looks like:

date: 1633186879027

I have a date like 2021-08-27T19:00:38.000+00:00 , and I save it like so:

date: "2021-08-27T19:00:38.000+00:00"

in the database looks like:

date: "2021-08-27T19:00:38.000+00:00"

and it's not what I want cause it's a string and I can't sort stuff by date then.

so i tried to save it like so:

date: new Date("2021-08-27T19:00:38.000+00:00")

and in the database looks like:

date: 2021-08-27T19:00:38.000+00:00

without brakets, as Mongodb does for strings, so must not be a string either

how am I supposed to save it so that it looks like the first one (1633186879027)? because I then need to sort stuff by date and I think that's the correct format to use?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

MongoDB stores data using BSON. The definition is here

A datetime is stored as the number of milliseconds since 1970-01-01 using a 64-bit integer.

Date.now() returns an integer, so the sample data would be stored as BSON type \x12 with the value 1633186879027.

new Date("2021-08-27T19:00:38.000+00:00") would be stored as BSON type \x09 with the value 1630090838000.

The output date: 2021-08-27T19:00:38.000+00:00 was generated by the driver or application on the client side after it was retrieved.

MongoDB directly support sorting on dates.

Also note that if you have dates stored as strings like "2021-08-27T19:00:38.000+00:00", a lexicographical sort of those string would put them in chronological order.

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