首页 > 代码库 > mongo聚合例子

mongo聚合例子

聚合语句

db.onofflineUserHistory.aggregate([
{$match:{"offlineTime":{$gt:ISODate("2016-09-13T01:54:55Z"), "$lt":ISODate("2016-09-13T10:05:55Z")},"logType":"off"}},
{$project:{updateTime:1,offlineTime:1,offTimeInc:{$add:[‘$offlineTime‘,1800000]},mac:1}},
{$project:{updateTime:1,offlineTime:1,offTimeInc:1,mac:1,eq:{$cond:[{$gt:[‘$updateTime‘,‘$offTimeInc‘]}, 1, 0]}}},
{$match:{eq:1}}
])

聚合条数语句
db.tableName.aggregate([{$match:{
"offlineTime":{$gt:ISODate("2016-09-13T01:54:55Z"), "$lt":ISODate("2016-09-13T10:05:55Z")},"logType":"off"}},{$project:{updateTime:1,offlineTime:1,offTimeInc:{$add:[$offlineTime,1800000]},mac:1}},{$project:{updateTime:1,offlineTime:1,offTimeInc:1,mac:1,eq:{$cond:[{$gt:[$updateTime,$offTimeInc]}, 1, 0]}}},{$match:{eq:1}},{$group:{_id:null, total:{$sum:1}}}])

 

mongo聚合例子