Home > Net >  Compare hour of dates in mongo find method
Compare hour of dates in mongo find method

Time:10-30

I have mongo documents containing a field createAt date. I would like to search for all documents where

the hour of createAt at 8 o'clock in the morning of everyday (between 8:00am and 8:00am)

, but I have no clue how to write the query.

In MySQL I can write it like this:

select * from table where hour(createAt)= 8

document in collection

CodePudding user response:

You can use $hour with $expr.

db.collection.find({
  $expr: {
    $eq: [
      {
        $hour: "$createdAt"
      },
      8
    ]
  }
})

Sample Mongo Playground

CodePudding user response:

Your question has already been resolved.

I wrote you an example on the playground.

  • Related