2

I'm clear on how to query documents within a range, but if I want, say, all docs in a collection created at 3PM (assuming I have a field with that stored of course - lets call it createdAt), what would that query look like? At present these dates are stored in my collection as JavasSript Date objects.

NeNaD
  • 18,172
  • 8
  • 47
  • 89
Baron Young
  • 181
  • 2
  • 13
  • 1
    Does this answer your question? [MongoDB/Mongoose querying at a specific date?](https://stackoverflow.com/questions/11973304/mongodb-mongoose-querying-at-a-specific-date) – Matt Oct 22 '22 at 03:13
  • Check out the [date expression operators](https://www.mongodb.com/docs/manual/reference/operator/aggregation/#date-expression-operators) such as `$hour` – user20042973 Oct 22 '22 at 03:42

1 Answers1

2

You can do it like this:

db.collection.aggregate([
  {
    "$match": {
      "$expr": {
        "$eq": [
          {
            "$hour": "$createdAt"
          },
          15
        ]
      }
    }
  }
])

Working example

NeNaD
  • 18,172
  • 8
  • 47
  • 89