mongodb聚合框架组字段匹配

时间:2016-07-14 02:23:44

标签: mongodb aggregation-framework

"_id" : ObjectId("576bb4147aac2b0c4ca47dfe"), 
"IdUser" : NumberInt(1), 
"UserName" : "PeterPan", 
"Age" :  NumberInt(20),  
"IMEI" : "123456789"
"LastLogin" : ISODate("2016-07-09T06:38:15.260+0000"),

"_id" : ObjectId("576bb4147aac2b0c4ca47dfd"), 
"IdUser" : NumberInt(2), 
"UserName" : "beckham", 
"Age" :  NumberInt(30),  
"IMEI" : "123456789"
"LastLogin" : ISODate("2016-07-06T06:38:15.260+0000"),

"_id" : ObjectId("576bb4147aac2b0c4ca47dfg"), 
"IdUser" : NumberInt(3), 
"UserName" : "Pham", 
"Age" :  NumberInt(25),  
"IMEI" : "123456789"
"LastLogin" : ISODate("2016-07-08T06:38:15.260+0000"),

我想检查所有imei副本,$ match lastlogin> 8/72016,返回0结果 (请帮帮我)

1 个答案:

答案 0 :(得分:0)

以下内容将为您提供IMEI字段& LastLogin值> = 08/07/2016:

db.devices.aggregate(
    {"$group" : { "_id": "$IMEI", "count": { "$sum": 1 } ,last: { $last: "$LastLogin" }} },
    {"$match": {"_id" :{ "$ne" : null } , "last":{"$gte": ISODate("2016-07-08T00:00:00.000Z") }, "count" : {"$gt": 1} } }, 
    {"$project": {"IMEI" : "$_id", "_id" : 0} }
)

check mongodb aggregation