> db.test.find();
{ "_id" : ObjectId("5d0c9094e07a14a96b41b7c9"), "id" : 20162, "create_time" : 1554195600, "time" : "2019-04-02 17:00:00" }
{ "_id" : ObjectId("5d0c9094e07a14a96b41b7ca"), "id" : 20159, "create_time" : 1552635000, "time" : "2019-03-15 15:30:00" }
{ "_id" : ObjectId("5d0c9094e07a14a96b41b7cb"), "id" : 20160, "create_time" : 1552632900, "time" : "2019-03-15 14:55:00" }
{ "_id" : ObjectId("5d0c9094e07a14a96b41b7cc"), "id" : 20161, "create_time" : 1552632900, "time" : "2019-03-15 14:55:00" }
{ "_id" : ObjectId("5d0c9095e07a14a96b41b7cd"), "id" : 20158, "create_time" : 1542798000, "time" : "2018-11-21 19:00:00" }
1、按天聚合查询,根据create_time时间戳字段
db.test.aggregate( [{
            $project: { 
                    dt:{$dateToString:{format:"%Y-%m-%d",date:{$add:[ISODate("1970-01-01T00:00:00Z"),{$multiply:["$create_time",1000]}]}}}
            }},{
            $group: {
            _id:"$dt"
            ,count:{$sum:1}
            }},]);

2、按天聚合查询,根据time字符串字段
db.test.aggregate([{$project:{"new_string":{$substr:["$time",0,9]}}},{$group:{_id:"$new_string",num:{$sum:1}}}])
3、拼接
concat
4、limit
db.test.aggregate([ {"$sort":{"value":1}}, {"$skip":2}, {"$limit":2} ]); 
5、out
将查询结果写入表中
db.test.aggregate([ {"$sort":{"value":1}}, {"$skip":2}, {"$limit":2} ,{$out:t10}])
 posted on 2020-08-27 19:56  xibuhaohao  阅读(2221)  评论(0编辑  收藏  举报