Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!