i have a question, i want to search by specific hours of the day,
Lets start:
My data schema sees like this:
{
"symbol": "Orange",
"timestamp": ISODate("2016-05-01T20:00:00.000Z"),
"price": 10
}
I have a lot of entries, and the value of price change minute by minute.
If i want to search, let's say, all days when the hours of the day are: 9 or 15, so the result must return all the entries 9 to 9:59 and 15 to 15:59
I try to do something like:
{ $match: { timestamp: { $hour: { $in: [array_hours] } } }
But obviously, don't work.
Any ideas?
I think you can use $hour, $day ... to break down the date in aggregation. For example:
my data:
db.orders.find()
{ "_id" : ObjectId("5760bf21b05d6825e05fa23d"), "item" : "test1",
"create_at" : ISODate("2016-04-30T11:00:00Z") }
{ "_id" : ObjectId("5760bf30b05d6825e05fa23e"), "item" : "test2",
"create_at" : ISODate("2016-04-30T12:00:00Z") }
{ "_id" : ObjectId("5760bf37b05d6825e05fa23f"), "item" : "test3",
"create_at" : ISODate("2016-04-30T13:00:00Z") }
my query:
db.orders.aggregate([{$project:{hour:{$hour:"$create_at"}}},
{$match:{hour:{"$in":[11,12]}}}])
{ "_id" : ObjectId("5760bf21b05d6825e05fa23d"), "hour" : 11 }
{ "_id" : ObjectId("5760bf30b05d6825e05fa23e"), "hour" : 12 }
Here is the doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With