4

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 }
        });
4

1 に答える 1

5

使用しているドキュメント構造(アイテムの配列)には、MapReduceまたはMongoDB2.2の新しいAggregationFramework$group()で簡単に実行できる関数の操作が必要になります。

集約フレームワークを使用した例を次に示します。

db.activity.aggregate(

    // Find matching documents first (can take advantage of index)
    { $match : {
        Loc: DBRef("location", ObjectId("501cb6d4e7e9a8f958f903c5"))
    }},

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

    // Group the stream by activity types for a user
    { $group : {
        _id : "$ActivityList.user",
        activity: { $push: "$ActivityList.type" } 
    }},

    // Unwind the activity types so they can be counted
    { $unwind : "$activity" },

    // Final grouping: count of action (entry or exit) for each user
    { $group : {
        _id : { user: "$_id", action: "$activity" },
        count: { $sum: 1 }
    }}
)

これにより、次のような結果が得られます。

{
    "result" : [
        {
            "_id" : {
                "user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb81")),
                "action" : "exit"
            },
            "count" : 6
        },
        {
            "_id" : {
                "user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb81")),
                "action" : "entry"
            },
            "count" : 3
        },
        {
            "_id" : {
                "user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb82")),
                "action" : "entry"
            },
            "count" : 2
        },
        {
            "_id" : {
                "user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb85")),
                "action" : "entry"
            },
            "count" : 1
        }
    ],
    "ok" : 1
}
于 2012-10-05T12:55:16.520 に答える