0

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

Yong Shun
  • 14,326
  • 3
  • 15
  • 32

2 Answers2

1

You can use $hour with $expr.

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

Sample Mongo Playground

Yong Shun
  • 14,326
  • 3
  • 15
  • 32
0

Your question has already been resolved.

I wrote you an example on the playground.

Dharman
  • 26,923
  • 21
  • 73
  • 125
Borislav
  • 1
  • 1