我该如何在Mongodb中查询一个位置的进出次数并按用户分组。上述集合有多个位置,用户可以进入或离开。
{ "ActivityList" : [
{ "type" : "entry",
"timestamp" : Date( 1344473257320 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "exit",
"timestamp" : Date( 1348792321111 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "entry",
"timestamp" : Date( 1348881701129 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "exit",
"timestamp" : Date( 1348942808700 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "entry",
"timestamp" : Date( 1348957400052 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "exit",
"timestamp" : Date( 1349024290729 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } } ],
"Loc" : { "$ref" : "location",
"$id" : ObjectId( "501cb6d4e7e9a8f958f903c5" ) },
"_id" : ObjectId( "502308a9e7e91ab176f6708e" ) }
我正在尝试类似这样的操作,但没有成功,请参考这里。
select a,b,sum(c) csum from coll where active=1 group by a,b
db.coll.group(
{key: { a:true, b:true },
cond: { active:1 },
reduce: function(obj,prev) { prev.csum += obj.c; },
initial: { csum: 0 }
});