我喜欢获取给定用户和给定位置的最新进入和退出时间戳。合集是这样的
{ "ActivityList" : [
{ "type" : "exit",
"timestamp" : Date( 1348862537170 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaeeede26fd298262bb80" ) } },
{ "type" : "entry",
"timestamp" : Date( 1348862546966 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaeeede26fd298262bb80" ) } },
{ "type" : "entry",
"timestamp" : Date( 1348870744386 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "exit",
"timestamp" : Date( 1348878233785 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } } ],
"Location" : { "$ref" : "loc",
"$id" : ObjectId( "4fd410f0e7e994b59054b824" ) },
"_id" : ObjectId( "4fe8f3c6e7e9ebe3697ee836" ) }
我尝试过这样的事情但不起作用
db.collection.group(
{
keyf: function(doc) {
return {
location :doc.Location._id,
userid : doc.ActivityList.user._id,
actiontype : doc. ActivityList.type
};
},
reduce: function(obj,prev) {
if (prev.maxdate < obj. ActivityList.timestamp) {
prev.maxdate = obj. ActivityList.timestamp;
}
},
initial: {maxdate:0}
});
谢谢你的帮助。