获取 objects 的数组,其中布尔值为真
get array of objects where boolean is true
这是我的collection
{
"_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"})
我怎样才能只获得免费剧季和剧集?
您的问题对于包含非免费剧集的免费季的预期结果含糊不清,反之亦然,非免费季中的免费剧集。因此,我假设您只想要免费且属于免费季的剧集。这就是以下内容(未测试):
_.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;
}, []);
如果您想要所有免费剧集或免费季的一部分:
_.reduce(db.courses.findOne({_id:"bRu9ExERzz8PCDwRp"}).Seasons, function(all, s) {
if (s.free_for_all) {
all = all.concat(s.Episodes);
} else {
all = _.reduce(s.Episodes, function(memo, e) {
if (e.free_for_all) memo.push(e);
return memo;
}, all);
}
return all;
}, []);
这是我的collection
{
"_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"})
我怎样才能只获得免费剧季和剧集?
您的问题对于包含非免费剧集的免费季的预期结果含糊不清,反之亦然,非免费季中的免费剧集。因此,我假设您只想要免费且属于免费季的剧集。这就是以下内容(未测试):
_.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;
}, []);
如果您想要所有免费剧集或免费季的一部分:
_.reduce(db.courses.findOne({_id:"bRu9ExERzz8PCDwRp"}).Seasons, function(all, s) {
if (s.free_for_all) {
all = all.concat(s.Episodes);
} else {
all = _.reduce(s.Episodes, function(memo, e) {
if (e.free_for_all) memo.push(e);
return memo;
}, all);
}
return all;
}, []);