【发布时间】:2018-12-17 21:06:36
【问题描述】:
const userSchema = new Schema(
{
_id: Schema.Types.ObjectId,
name: String,
posts: [{ type: Schema.Types.ObjectId, ref: "Post" }],
following: [{ type: Schema.Types.ObjectId, ref: "User" }]
}
};
我想从 'following' 数组中的所有用户中提取所有帖子,将它们放入一个数组中,对它们进行排序,然后显示前 20 个。我想知道这是否可以在光标内进行,或者我是否可以必须将其加载到内存中。
function createFeed(user) {
User.findOne({ name: user })
.populate({
path: "following",
populate: {
path: "posts"
}
})
//put all the posts into one array
.sort(...) //sort by time created
.limit(...) //only get the newest n posts
.exec((err, result) => {
if (err) console.log("error", err);
console.log("result", //sorted tweets array);
});
};
(我不想过滤我的“帖子”集合中的所有帖子以检查它们是否由用户制作,因为那样会更昂贵)
【问题讨论】:
标签: javascript node.js mongodb mongoose