2017-05-26 6 views
1

ですが、私は(どちらかUSER1またはuser2の中に)最も外観を持っているユーザー知りたいのですが、次の形式グループ2つの可能なフィールドから値を使用して

 {user1: "a", user2: "b"}, 
     {user1: "a", user2: "c"}, 
     {user1: "b", user2: "d"}, 
     {user1: "b", user2: "c"}, 
     {user1: "b", user2: "e"}, 
     {user1: "c", user2: "f"} 

にマッチコレクションを持っているとしましょう。結果は、この形式で出現回数順に並べる必要があります。

{"user": "b", count:4}, 
      {"user": "c", count:3}, 
      {"user": "a", count:2}, 
      {"user": "d", count:1}, 
      {"user": "f", count:1}, 
      {"user": "e", count:1} 

私は2つのフィールドの値にグループ化することができ方法はありますか?

何かmatch.aggregateよう({$基:{_id:{$または:[ "USER1"、「USER2]}}、カウント:{$合計:1}})

答えて

7
db.match.aggregate([ 
    {$project: { user: [ "$user1", "$user2" ]}}, 
    {$unwind: "$user"}, 
    {$group: {_id: "$user", count: {$sum:1}}} 
]) 

第1段プロジェクト各文書

{user: ["a", "b"]}, 
    {user: ["a", "c"]}, 
    {user: ["b", "d"]}, 
    ... 

次は、私たちは、配列

{user:"a"}, 
    {user:"b"}, 
    {user:"a"}, 
    {user:"c"}, 
    {user:"b"}, 
    ... 

そして最後に、単純なグループ化

+1

大漁のプログラミングをおくつろぎください。これは3.2以降で許可されたことを忘れていた –

0

基本的概念は、そこからアレイ及び作業上$mapである:ユーザーの配列に

db.collection.aggregate([ 
    { "$project": { 
    "_id": 0, 
    "user": { "$map": { 
     "input": ["A","B"], 
     "as": "el", 
     "in": { 
     "$cond": { 
      "if": { "$eq": [ "$$el", "A" ] }, 
      "then": "$user1", 
      "else": "$user2" 
     } 
     } 
    }} 
    }}, 
    { "$unwind": "$user" }, 
    { "$group": { 
    "_id": "$user", 
    "count": { "$sum": 1 } 
    }} 
]) 
0
Let us take an example and go through 

db.users_data.find(); 
{ 
    "_id" : 1, 
    "user1" : "a", 
    "user2" : "aa", 
    "status" : "NEW", 
    "createdDate" : ISODate("2016-05-03T08:52:32.434Z") 
}, 
{ 
    "_id" : 2, 
    "user1" : "a", 
    "user2" : "ab", 
    "status" : "NEW", 
    "createdDate" : ISODate("2016-05-03T09:52:32.434Z") 
}, 
{ 
    "_id" : 3, 
    "user1" : "b", 
    "user2" : "aa", 
    "status" : "NEW", 
    "createdDate" : ISODate("2016-05-03T10:52:32.434Z") 
}, 
{ 
    "_id" : 4, 
    "user1" : "b", 
    "user2" : "ab", 
    "status" : "NEW", 
    "createdDate" : ISODate("2016-05-03T10:52:32.434Z") 
}, 
{ 
    "_id" : 5, 
    "user1" : "a", 
    "user2" : "aa", 
    "status" : "OLD", 
    "createdDate" : ISODate("2015-05-03T08:52:32.434Z") 
}, 
{ 
    "_id" : 6, 
    "user1" : "a", 
    "user2" : "ab", 
    "status" : "OLD", 
    "createdDate" : ISODate("2015-05-03T08:52:32.434Z") 
}, 
Then 
db.users_data.aggregate([ 
     {"$group" : {_id:{user1:"$user1",user2:"$user2"}, count:{$sum:1}}} ]) 
    ]) 
will give the resuls as 
{ "_id" : { "user1" : "a", "user2" : "aa" }, "count" : 2} 
{ "_id" : { "user1" : "a", "user2" : "ab" }, "count" : 2} 
{ "_id" : { "user1" : "b", "user2" : "aa" }, "count" : 1} 
{ "_id" : { "user1" : "b", "user2" : "ab" }, "count" : 1} 

Thus grouping by multiple ids are possible 
Now one more variation 
db.users_data.aggregate([ 
     {"$group" : {_id:{user1:"$user1",user2:"$user2",status:"$status"}, count:{$sum:1}}} ]) 
    ]) 

will give the resuls as 
{ "_id" : { "user1" : "a", "user2" : "aa","status":"NEW" }, "count" : 1} 
{ "_id" : { "user1" : "a", "user2" : "ab","status":"NEW" }, "count" : 1} 
{ "_id" : { "user1" : "b", "user2" : "aa","status":"NEW" }, "count" : 1} 
{ "_id" : { "user1" : "b", "user2" : "ab","status":"NEW" }, "count" : 1} 
{ "_id" : { "user1" : "a", "user2" : "aa","status":"OLD" }, "count" : 1} 
{ "_id" : { "user1" : "a", "user2" : "ab","status":"OLD" }, "count" : 1} 

Hope that helps 

ハッピー

関連する問題