MongoDB ISODate Breakdown

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 }

Here is the doc

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容