MongoDB ISODate Breakdown
2016-06-15 本文已影响67人
Yves_lau
How to breakdown the ISODate, and just query the fuzzy hour range.
For example : query 11:00 - 12:00 data not matter which month,day, or year...
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 }