Baron Young
Baron Young

Reputation: 311

How can I query for all documents that were created at a specific time each day?

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.

Upvotes: 2

Views: 195

Answers (1)

NeNaD
NeNaD

Reputation: 20354

You can do it like this:

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

Working example

Upvotes: 2

Related Questions