数组中的Mongoose布尔查询返回所有记录

时间:2015-08-26 19:26:23

标签: mongodb mongoose

我正在试图弄清楚如何选择符合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. 
});

非常感谢任何想法。

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'
});