포럼/카테고리를 표시하려고합니다. 나는 최신 게시물을 얻을 필요가있다. 문제는 최신 게시물뿐만 아니라 각 게시물에 대한 사용자 데이터도 필요하다는 것입니다.mongodb 모든 문서에 대한 사용자 정보 얻기
db.post.find({
"inForum": forumID,
},
{
'sort': [['date', -1]]
},
function(err, cursor) {
cursor.count(function(err, count) {
cursor.skip(skip).limit(20).toArray(function(err, posts) {
var complete = _.after(nodes.length, function() {
res.send(posts)
});
// for every post get its author info and the latest post info
posts.forEach(function (post) {
var users = _.pluck(posts, 'user');
user.load(users, function (profiles) {
_.each(posts,
function(post, k) {
if (profiles[post.user]) post.fieldAvatar = profiles[post.user].fieldAvatar;
});
if (post.latestReply) {
post.load(post.latestReply.id, function (latestReply) {
if (latestReply) post.latestReply = latestReply
complete()
})
}
else {
complete()
}
})
})
});
})
})
이것은 내가하고있는 일이며, 나에게는 정말로 느리고/정말로 유쾌한 것처럼 보입니다. 이 작업을 올바르게 수행하고 있으며이를 가속화하기위한 조언이 있습니까?
감사합니다.
그래, 실제로이 최적화가 다른 곳에서 코드에 있지만이 예제에 포함시키는 것을 잊었습니다. user.load는 루프를 수행하는 것이 아니라 ": users"에서 $를 사용합니다. – Harry
언급 한 것 중 하나 (선호하는 첫 번째)를 수행하면 잘됩니다. – alessioalex
고마워, 흥미롭게도 나는 그것을 사용자와 함께했지만 배열 대신에 최신 게시물을위한 루프를 사용했다. 어리석은 저, 고마워요 – Harry