获取boolean为true的对象数组

时间:2015-11-24 16:25:17

标签: mongodb meteor nosql

这是我的收藏品

{
      "_id": "bRu9ExERzz8PCDwRp",
      "persian_title": "عنوان پارسی",
      "english_title": "english title",
      "price": 5000,
      "offer_price": 2000,
"free_for_all": false,
      "is_offer": false,
      "author_id": "JH3hJGsuYFnRLFLWY"
      "Seasons": [
        {
          "title": "intro",
          "free_for_all": false,
          "Episodes": [
            {
              "title": "first episode",
              "length": "12",
              "url": "0.mp4",
              "free_for_all": true
            },
            {
              "title": "second episode",
              "length": "05",
              "url": "1.mp4",
              "free_for_all": false
            }
          ]
        }
      ]
    }

我试图获得季节,free_for_all为真的剧集。 试过这个,但它没有用。

db.courses.find({_id:"bRu9ExERzz8PCDwRp"}, {
"Seasons": { 
        "$elemMatch": {
            "Episodes": {
                "$elemMatch": {
                    "free_for_all": true,
                }
            }
        }
    }
})

结果就像这个查询:

db.courses.find({_id:"bRu9ExERzz8PCDwRp"})

我怎样才能获得免费的季节和剧集?

1 个答案:

答案 0 :(得分:1)

关于非自由季节的自由季节的预期结果,反之亦然,非自由季节的自由情节,你的问题是模棱两可的。所以我假设你只想要免费的剧集并且是免费季节的一部分。以下内容将给您(未经测试):

shell

如果您想要所有免费或免费季节的剧集:

_.reduce(db.courses.findOne({_id:"bRu9ExERzz8PCDwRp"}).Seasons, function(all, s) {
     if (s.free_for_all) {
         all = _.reduce(s.Episodes, function(memo, e) {
              if (e.free_for_all) memo.push(e);
              return memo;
         }, all);
     }
     return all;
}, []);