我有一个查询来查找一个帐户并填充该帐户的子文档。
当我使用 populate path:"orders" 时,限制选项将起作用。但是,如果我使用填充路径“orders.order”,则选项限制将不起作用。
我该怎么办?
我的架构是这样的:
var AccountSchema = new mongoose.Schema({
_id:id,
orders:{type:[{
order:{type: mongoose.Schema.Types.ObjectId, ref:'Order'}
}]}
});
var findOrderByUserId = function(accountId,index,count,callback){
var limit = index*count;
console.log(limit);
Account.findOne({_id:accountId}).populate({
path:'orders.order',//If path is orders, then limit will work
options:{
limit:limit
}
}).exec(function (err, doc) {
if (err) {
console.log(err);
callback(err);
}
console.log(doc);
var array = [];
for(var i=limit - count;i<doc.orders.length;i++){
if(doc.orders[i]!=null){
array.push(doc.orders[i]);
}
else{
break;
}
}
callback(doc);
})
}
Run Code Online (Sandbox Code Playgroud)
limit仅适用于顶级文档,以控制您需要使用的文档内的数组$slice,因此这意味着您需要重建您的查询,如下所示(mongo shell):
db.posts.find( {}, { comments: { $slice: 5 } } )
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅此帖子- 和示例:
query.slice('comments', 5)
// or...
query.where('comments').slice(5)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6440 次 |
| 最近记录: |