填充有限数量的文档

考虑这个示例架构

var BookSchema = new Schema({ title: { type: String }, user: { type: Schema.ObjectId, ref: 'User' } }); 

假设我在MongoDb book集合中有10条logging
当查询书籍列表时,我只想填充前三本书

 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个被填充;

       

网上收集的解决方案 "填充有限数量的文档"

不知道“前三名”是从哪里来的,但如果你只是想“三本书”,但是你确定(而且更好的一种),那么你需要解决这个问题,所以只有“三”结果是填充的,其他结果得不到相同的处理。

因此,您只需在结果数组的“部分”中对结果进行“内部”操作即可:

 Book.find().exec(function(err,books) { User.populate( books.slice(0,3), // get first 3 array items { "path": "user", "select": "displayName" }, // populate options function(err,part) { books = part.concat(books.slice(-(books.length-3))); console.log( JSON.stringify( books, undefined, 2 ) ); } ); }); 

正如你所看到的,你可以通过手动调用User模型中的.populate()的forms,并且只需要填充数组响应的“部分”,然后用整个响应重新join。

作为一个更长的工作例子,有这样的:

 var async = require('async'), mongoose = require('mongoose'), Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/poptest'); var items = [ "one", "two", "three", "four","five", "six", "seven", "eight", "nine", "ten" ]; var itemSchema = new Schema({ name: String }); var dataSchema = new Schema({ name: String, items: [{ type: Schema.Types.ObjectId, ref: 'Item' }] }); var Item = mongoose.model( 'Item', itemSchema ); var Data = mongoose.model( 'Data', dataSchema ); async.series( [ function(callback) { async.each([Item,Data],function(model,callback) { model.remove({},callback); },callback); }, function(callback) { async.each([Item,Data],function(model,callback) { async.each(items,function(item,callback) { model.create({ name: item },callback); },callback); },callback); }, function(callback) { async.waterfall( [ function(callback) { Item.find({ name: { "$in": ["one","two","three"] } }) .exec(callback); }, function(itemList,callback) { Data.find().exec(function(err,datas) { callback(err,itemList,datas); }); }, function(itemList,datas,callback) { async.each(datas,function(data,callback) { itemList.forEach(function(item) { data.items.push(item._id); }); data.save(callback) },callback); } ], callback ); }, function(callback) { Data.find().exec(function(err,data) { if (err) callback(err); Item.populate(data.slice(0,3),'items',function(err,part) { if (err) callback(err); data = part.concat(data.slice(-(data.length-3))); console.log(data); callback() }); }); } ], function(err) { if (err) throw err; mongoose.disconnect(); } ); 

其中产生的输出只显示前三个结果:

 [ { _id: 55dc369e584563b619de221e, name: 'one', __v: 1, items: [ { _id: 55dc369e584563b619de2214, name: 'one', __v: 0 }, { _id: 55dc369e584563b619de2215, name: 'two', __v: 0 }, { _id: 55dc369e584563b619de2216, name: 'three', __v: 0 } ] }, { _id: 55dc369e584563b619de221f, name: 'two', __v: 1, items: [ { _id: 55dc369e584563b619de2214, name: 'one', __v: 0 }, { _id: 55dc369e584563b619de2215, name: 'two', __v: 0 }, { _id: 55dc369e584563b619de2216, name: 'three', __v: 0 } ] }, { _id: 55dc369e584563b619de2220, name: 'three', __v: 1, items: [ { _id: 55dc369e584563b619de2214, name: 'one', __v: 0 }, { _id: 55dc369e584563b619de2215, name: 'two', __v: 0 }, { _id: 55dc369e584563b619de2216, name: 'three', __v: 0 } ] }, { _id: 55dc369e584563b619de2221, name: 'four', __v: 1, items: [ 55dc369e584563b619de2214, 55dc369e584563b619de2215, 55dc369e584563b619de2216 ] }, { _id: 55dc369e584563b619de2222, name: 'five', __v: 1, items: [ 55dc369e584563b619de2214, 55dc369e584563b619de2215, 55dc369e584563b619de2216 ] }, { _id: 55dc369e584563b619de2223, name: 'six', __v: 1, items: [ 55dc369e584563b619de2214, 55dc369e584563b619de2215, 55dc369e584563b619de2216 ] }, { _id: 55dc369e584563b619de2224, name: 'seven', __v: 1, items: [ 55dc369e584563b619de2214, 55dc369e584563b619de2215, 55dc369e584563b619de2216 ] }, { _id: 55dc369e584563b619de2225, name: 'eight', __v: 1, items: [ 55dc369e584563b619de2214, 55dc369e584563b619de2215, 55dc369e584563b619de2216 ] }, { _id: 55dc369e584563b619de2226, name: 'nine', __v: 1, items: [ 55dc369e584563b619de2214, 55dc369e584563b619de2215, 55dc369e584563b619de2216 ] }, { _id: 55dc369e584563b619de2227, name: 'ten', __v: 1, items: [ 55dc369e584563b619de2214, 55dc369e584563b619de2215, 55dc369e584563b619de2216 ] } ] 

我认为你必须添加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); }); };