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

361
Visualizações
MongoDB v5.0.5 query using hour only

First occasion experimenting with dates and times in MongoDB.

Currently, I have correctly inserted UTC dates into my documents:

{
 "Value": 10
 "DateTime": {
        "$date": "2013-08-23T08:00:00.000Z"
    },
}

I want to query the data to return all of the 'value' and datetime fields for all documents in which the hour is 08. I have gotten as far as:

db.readings.aggregate([{$project:{hour:{$hour:"$DateTime"}}},{$match:{hour:{"$in":[08]}}}])

Which returns the _id and "hour": 8 for all matching entries but I'm confused at how to proceed. It seems an unnecessarily complicated way to search and so I wonder if I am barking up the wrong tree here? Admittedly, I am somewhat out of my depth so some guidance would be appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can try $expr expression operator to use aggregation operators ($hour) in query,

  • $expr to use aggregation operators
  • $hour will return an hour from the date
  • $eq to match hour and input hour 8
db.collection.find({
  $expr: {
    $eq: [
      { $hour: "$DateTime" },
      8
    ]
  }
})

Playground

over 4 years ago · Santiago Trujillo 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