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

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

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 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