피드가있는 컬렉션이 있습니다. 서류는 다음과 같이 구성되어 있습니다 : MongoDB (Node JS), 계산 된 필드를 전달 된 변수를 사용하는 쿼리 결과에 올바르게 추가하는 방법
{
_id: '123',
title: 'my title',
openedBy: ['321', '432', '543'] // ID of users
}
은 그럼 사용자가 :
사용자가 가져온 피드를 열었다 친구의 수를 얻을 수 있습니다 내가 달성하고자하는
{
_id '321',
friends: ['432'] // ID of users
}
. 나는 이제 mapReduce를 사용하여 피드를 가져 오는 사용자의 친구를 전달합니다. 나는 단지 그 자체를 방출 반환하여 줄로 내가 제대로하고있는 중이 야 생각하지 않는다 나는 종료 자에 일반 쿼리 결과에 결과를 다시 변환해야합니다 :
db.collection(collectionName).mapReduce(function() {
var openedByFriendsLength = 0;
for (var x = 0; x < friends.length; x++) {
if (this.openedBy.indexOf(friends[x]) >= 0) {
openedByFriendsLength++;
}
}
emit(this._id, {
title: this.title,
openedByLength: this.openedBy.length,
openedByFriendsLength: openedByFriendsLength
});
}, function (key, emits) {
return emits[0];
}, {
out: 'getFeeds',
scope: {
friends: user.friends
},
}, function (err, collection) {
collection.find().toArray(function (err, feeds) {
// Convert the _id/value to a normal find result
var resultFeeds = [];
for (var x = 0; x < feeds.length; x++) {
resultFeeds.push(feeds[x].value);
resultFeeds[resultFeeds.length - 1]._id = feeds[x]._id;
}
callback(err, resultFeeds);
});
});
나는 집계 살펴 보았다,하지만 난 똑같은 일을하는 방법을 알아낼 수 없습니다. 아니면 여기서 문서의 구조가 모두 잘못 되었습니까?
답장을 보내 주셔서 감사합니다.