美文网首页
MongoDB ISODate Breakdown

MongoDB ISODate Breakdown

作者: Yves_lau | 来源:发表于2016-06-15 11:11 被阅读67次

    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

    相关文章

      网友评论

          本文标题:MongoDB ISODate Breakdown

          本文链接:https://www.haomeiwen.com/subject/cltpdttx.html