【发布时间】:2016-04-22 23:22:26
【问题描述】:
MongoDB 3.0.7 和 Mongoose 4.3.4。
架构:
var schema = new mongoose.Schema({
confirmed: { type: Boolean, default: false },
moves: [new mongoose.Schema({
name: { type: String, default: '' },
live: { type: Boolean, default: true }
})]
});
mongoose.model('Batches', schema);
查询:
var Batch = mongoose.model('Batches');
var query = {
confirmed: true,
moves: {
$elemMatch: {
live: true
}
}
};
Batch.find(query).exec(function(err, batches){
console.log('batches: ', batches);
});
我需要返回 confirmed 的所有批次,以及返回的批次中的所有移动 live。
目前,上面只返回 confirmed 批次(这是我想要的),但 all 每个返回批次中的 moves (这不是我想要的) .所以live 标志的移动限制不起作用。
如何限制返回的子文档..?
理想情况下,我希望将控制query 中返回的数据的所有内容都传递给find,而不必在Batch 上调用更多方法。
【问题讨论】:
标签: javascript mongodb mongoose