我试图不仅过滤字段而是过滤数组字段中的对象。请参阅下面的示例,对于细分字段,我希望我的客户只能在细分数组中获取那些isReleased === true
示例架构
{
_id: 123456,
segments: [
{
name: 'Type A',
isReleased: false,
},
{
name: 'Type B',
isReleased: false,
},
{
name: 'Type C',
isReleased: true,
}
],
}
基本上,我希望我的客户能够接收,
{
_id: 123456,
segments: [
{
name: 'Type C',
isReleased: true,
}
],
}
答案 0 :(得分:0)
您可以使用$elemMatch运算符。查询将是这样的:
var Realms = mongoose.model('realms');
Realms.findOne({realmName: realm}, function (err, realm) {
realm.posts.id(postId).whosGoing.push(thisChar); //pushing it here no problem
console.log(realm.posts.id(postId).whosGoing); //when i log it here both objects are in the array just fine
console.log(realm); //but when i log the realm here, the item i just pushed is not in there, so of course if I realm.save() it wouldnt take effect...
realm.save();
})
您可以查看链接上的示例以获取更多详细信息。