我正在试图弄清楚如何选择符合Mongoose标准的数组中的某些对象。
假设以下记录:
var Record = {
records : [{name : "you can't look', isPublic : false},
{name : "ok you can look', isPublic : true},
{name : "you can look here too', isPublic : true}]
}
以下查询:
Record.find({'records.isPublic' : true}, function (error, response) {
//This returns all the records, rather than just 0 and 2.
});
非常感谢任何想法。
答案 0 :(得分:0)
使用db.collection.aggregate
,$unwind
和$match
。
答案 1 :(得分:0)
尝试类似的东西:
Record.find({}, {'records': {$elemMatch: {'isPublic': 'true'}}}), function (error, response) {
// finds everything, but projects only those which 'isPublic': 'true'
});