【发布时间】:2017-07-13 15:34:50
【问题描述】:
我有 3 个集合:用户、帖子和评论。 Posts 有多个 cmets。 我想要获取 50 个帖子,填充作者,填充 cmets,但我只想要按日期(_id)排序的前 2 个投票最多的 cmets
const PostSchema = new Schema({
author: {
type: Schema.Types.ObjectId,
ref: 'User'
},
content: String,
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}]
});
const Post = mongoose.model('Post', PostSchema);
const CommentSchema = new Schema({
author: {
type: Schema.Types.ObjectId,
ref: 'User'
},
content: String,
votes: Number
});
const Comment = mongoose.model('Comment', CommentSchema);
Post
.find({})
.limit(50)
.populate('author')
.populate('comments')
...
我不知道如何实现这一点。
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query mongoose-populate