Group result by 15 minutes time interval in MongoDb

后端 未结 5 2031
野趣味
野趣味 2020-11-21 23:49

I have a \"status\" collection like this strcture -

{
    _id: ObjectId(\"545a0b63b03dbcd1238b4567\"),
    status: 1004,
    comment: \"Rem dolor ipsam place         


        
5条回答
  •  无人共我
    2020-11-22 00:07

    A little more beautiful for mongo db.version() < 3.0

    db.collection.aggregate([
        {$match: {created_at:{$exists:1}}},
        {$group: {
            _id: {$add:[
                {$dayOfYear: "$created_at" },
                {$multiply: [{$year: "$created_at"}, 1000]}
            ]},
            count: {$sum: 1 }
        }},
        {$sort:{_id:-1}}
    ])
    

提交回复
热议问题