2

我喜欢获取给定用户和给定位置的最新进入和退出时间戳。合集是这样的

{ "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}
});

谢谢你的帮助。

4

1 回答 1

2

简单的$group不适用于您的数据结构和查找/过滤数组中的最大值。您将不得不迭代数组以找到最大值,通过检索文档并在应用程序代码中迭代可以更有效地完成。

MongoDB 2.2 中一种可能的服务器查询方法是使用新的聚合框架

db.activity.aggregate(

    // Find matching location documents first (can take advantage of index)
    { $match : {
        Location: {
            "$ref" : "loc", 
            "$id" : ObjectId("4fd410f0e7e994b59054b824")
        }
    }},

    // Unwind the ActivityList arrays as a document stream
    { $unwind : "$ActivityList" },

    // Filter activities to the user reference of interest
    { $match : {
       'ActivityList.user': {
            "$ref" : "userProfile",
            "$id" : ObjectId("4fdeaeeede26fd298262bb80")
        } 
    }},

    // Group the stream by activity types, and get the timestamp for the latest of each
    { $group : {
        _id : "$ActivityList.type",
        latest: { $max: '$ActivityList.timestamp' }
    }}
)

样本结果:

{
    "result" : [
        {
            "_id" : "entry",
            "latest" : ISODate("2012-09-28T20:02:26.966Z")
        },
        {
            "_id" : "exit",
            "latest" : ISODate("2012-09-28T20:02:17.170Z")
        }
    ],
    "ok" : 1
}
于 2012-10-08T14:14:52.450 回答