考虑这个示例模式
var BookSchema = new Schema({
title: {
type: String
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
假设我在MongoDb书籍收藏中有10条记录 在查询书籍列表时,我只想填写前三本书
exports.list = function(req, res) {
Book.find()
.populate('user', 'displayName') //populate only top 3 books here (10 in db)
.exec(function(err, books) {
res.json(books);
});
};
我该怎么做?
我想要所有10个文件,但只填充前3个;
答案 0 :(得分:1)
我认为你必须添加options
属性:
exports.list = function (req, res) {
Book.find()
.populate({
path: 'user',
select: 'displayName',
options: {
limit: 3
}
}) //populate only top 3 books here (10 in db)
.exec(function (err, books) {
res.json(books);
});
};