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

Time:10-23

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.

CodePudding user response:

You can do it like this:

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

Working example

  • Related