後ろの関係でソートするユーザーのリストを作成したいと思います。 、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つのコレクションでは動作しません。
結果では、私は新しい結合されたコレクションの新しいフィールドを設定できるキューを返す必要があります - 体重。このフィールドで並べ替えることができます。
ちょうどここに似たいくつかのことを答えて、いくつかのデータと、それらのデータから、あなたの予想される結果 – Khang
http://stackoverflow.com/q/40868364/2683814を記載してください。 – Veeram
@veeramご返信ありがとうございます。それは有用な例です。しかし、それは正確には機能しません。私は私の仕事の場合にあなたの例を使用しようとした私の質問を更新しました。たぶん何かが理解できないので、私に尋ねるかもしれません。ありがとうございました! –