2016-11-29 1 views
0

後ろの関係でソートするユーザーのリストを作成したいと思います。 、Mongoの1つのパイプラインに少数のコレクションを結合してソートする方法

2番目の 'からの要求を受けた': '送信された要求へ' を

最初に - これを行うために

は、私が順にソートする3つのコレクションのIDを比較する必要があります

第三:「友人、

第四: 'その他'

更新:ユーザーの背後関係を認識 入力

コレクション

db.friends.find({userId: currentUser}); 
    // {"_id" : "qwC7LrtaZtQsShtzT", "userId" : "rbrwhh2AQY8mzmzd3", "friendId" : "4y7pG7p2gcGgm5ayj",} 
db.requests.find({userId: currentUser}); 
    // {"_id" : "PgC7LrtaZtQsShtzT", "userId" : "tHuxnWxFLHvcpRgHb", "requesterId" : "jZagPF7bd4aW8agXb",} 

主なすべてのユーザーとの収集とその情報

db.users.find({userId: currentUser}); 
     // {"_id" : "4y7pG7p2gcGgm5ayj", profile: {name: 'Andrey', second: 'Andrey'}} -> Same person(id) like in 'friends' collection. 
    // {"_id": "tHuxnWxFLHvcpRgHb", profile: {name: 'Erick', second: 'Erick'} -> same person who have receive request. 

そして今、私がする必要が集める 'users' colle以前の2つのコレクション(友人、要求)と一致するスコアを定義します。

私はこの

db.users.aggregate([ 
    // here i tried to compare _id and requesterId to fetch all who received request from me: 
    {$lookup: {from: 'requests', localField: "_id", foreignField: "requesterId", as: "sentRequest"}}, 
    {$unwind: '$sentRequest'}, 
    {$project: {_id: '$sentRequest.userId', profile: 1, count: {$add: [2]}}}, 
    // And here I tried to get all friends 
    {$lookup: {from: 'requests', localField: "_id", foreignField: "friendId", as: "friends"}}, 
    {$unwind: '$friends'}, 
    {$project: {_id: '$friends.userId', profile: '$profile', count: {$add: [3]}}} 

]); 

のように実行しようとしましたが、結果には、私は最後の部分だけ(友人)を取得し、別の問題

[ { _id: 'rbrwhh2AQY8mzmzd3', //this field is responds to person which I looking for 
profile: //But profile data is given from .users and its not respont to person with given id 
    { firstName: 'Andrey', 
    lastName: 'Andrey', 
    userImg: '/img/user.jpg', 
    userDescription: null }, 
    count: 3 } ] 

UPDATE

期待される結果

//Given: 3 collections 
db.users -> provide all additional info about all users 
db.requests -> groups IDs of two users 'requester' and 'receiver' 
db.friends -> groups IDs of two users 'friend' and 'user', document has pair where value are swapped (but it's not important in this task); 

//Need to combine all of this three collections in one queue, to sort users by their relationships between them, in kind of this list: 
// 1 - Requests; 2 - Friends; 3 - Other users 


// Expected result: 
[ 
    // I've got request From: 
    { "_id": "tHuxnWxFLHvcpRgHb", // _id must be equal to user who sent request and _id from db.users 
     "profile": // this value used from db.users 
     { 
      "firstName": "Ana", 
      "lastName": "de Armas", 
      "userImg": "/img/user.jpg", 
      "userDescription": null 
     }, 
      "weight": 4 // given value to make sorting then 
    }, 
    // I sent request To: 
    { "_id": "4y7pG7p2gcGgm5ayj", 
     "profile": 
     { 
      "firstName": "John", 
      "lastName": "Bonjovi", 
      "userImg": "/img/user.jpg", 
      "userDescription": null 
     }, 
      "weight": 3 
    }, 
    // My friend: 
    { "_id": "jZagPF7bd4aW8agXb", 
     "profile": 
     { 
      "firstName": "Jessica", 
      "lastName": "Alba", 
      "userImg": "/img/user.jpg", 
      "userDescription": null 
     }, 
      "weight": 2 
    }, 
    // Unknown user: 
    { "_id": "DdX8sPuAoZqKpa6nH", 
     "profile": 
     { 
      "firstName": "Sheldon", 
      "lastName": "Cooper", 
      "userImg": "/img/user.jpg", 
      "userDescription": null 
     }, 
      "weight": 1 
    } 
] 

私は$検索と集約を使用しようとした - が、その3つのコレクションでは動作しません。

結果では、私は新しい結合されたコレクションの新しいフィールドを設定できるキューを返す必要があります - 体重。このフィールドで並べ替えることができます。

+0

ちょうどここに似たいくつかのことを答えて、いくつかのデータと、それらのデータから、あなたの予想される結果 – Khang

+0

http://stackoverflow.com/q/40868364/2683814を記載してください。 – Veeram

+0

@veeramご返信ありがとうございます。それは有用な例です。しかし、それは正確には機能しません。私は私の仕事の場合にあなたの例を使用しようとした私の質問を更新しました。たぶん何かが理解できないので、私に尋ねるかもしれません。ありがとうございました! –

答えて

0

最新モンゴ3.4バージョンには、複数の集約パイプライン全体で同じデータを処理するために使用$facetを作成し、すべてのaggregrationからの単一出力に結果を結合します。

db.users.aggregate([ 
    { "$match": { "_id":1} }, 
    { 
     "$facet": { 
      "Received": [ 
       {$lookup: {from: 'requests', localField: "_id", foreignField: "userId", as: "receivedRequest"}}, 
       {$unwind: '$receivedRequest'}, 
       {$lookup: {from: 'users', localField: "receivedRequest.requesterId", foreignField: "_id", as: "receivedUser"}}, 
       {$project: {_id: '$receivedRequest.requesterId', profile: '$receivedUser.profile', weight: {$add: [4]}}} 
      ], 
      "Sent": [ 
       {$lookup: {from: 'requests', localField: "_id", foreignField: "requesterId", as: "sentRequest"}}, 
       {$unwind: '$sentRequest'}, 
       {$lookup: {from: 'users', localField: "sentRequest.userId", foreignField: "_id", as: "sendUser"}}, 
       {$project: {_id: '$sentRequest.userId', profile: '$sendUser.profile', weight: {$add: [3]}}} 
      ], 
      "Friends": [ 
       {$lookup: {from: 'friends', localField: "_id", foreignField: "userId", as: "friends"}}, 
       {$unwind: '$friends'}, 
       {$lookup: {from: 'users', localField: "friends.friendId", foreignField: "_id", as: "friendUser"}}, 
       {$project: {_id: '$friends.friendId', profile: '$friendUser.profile', weight: {$add: [2]}}} 
      ], 
      "Others": [ 
       {$lookup: {from: 'friends', localField: "_id", foreignField: "friendId", as: "others"}}, 
       {$unwind: '$others'}, 
       {$lookup: {from: 'users', localField: "others.userId", foreignField: "_id", as: "other"}}, 
       {$project: {_id: '$others.userId', profile: '$other.profile', weight: {$add: [1]}}} 
      ] 
     } 
    } 
]).pretty() 

サンプル出力:

{ 
     "Received" : [ 
       { 
         "_id" : 3, 
         "profile" : [ 
           { 
             "name" : "John" 
           } 
         ], 
         "weight" : 4 
       } 
     ], 
     "Sent" : [ 
       { 
         "_id" : 4, 
         "profile" : [ 
           { 
             "name" : "Jessica" 
           } 
         ], 
         "weight" : 3 
       } 
     ], 
     "Friends" : [ 
       { 
         "_id" : 2, 
         "profile" : [ 
           { 
             "name" : "Ana" 
           } 
         ], 
         "weight" : 2 
       } 
     ], 
     "Others" : [ 
       { 
         "_id" : 5, 
         "profile" : [ 
           { 
             "name" : "Sheldon" 
           } 
         ], 
         "weight" : 1 
       } 
     ] 
} 
+0

これは素晴らしい機能ですが、私は流星を使用しています。残念ながら、この改善は間もなくサポートされません。他の方法でこれをどのように解決できるのか分かりますか?私はifの配列をソートするために 'if else'ステートメントを使用しようとしましたが、配列から各要素の位置に関係なく、コレクションからデータを取得するときに標準の順序の場合に返します。 –

+0

投稿に試した内容を追加してください。私はそれを構築しようとすることができるかもしれません。 – Veeram

関連する問題