0

我试图过滤的不仅仅是字段,还有我的数组字段中的对象。参考下面的示例,对于分段字段,我希望我的客户只获取分段数组中的那些对象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,
        }
    ],
}
4

1 回答 1

0

You could use the $elemMatch operator. The query would be like this:

Schema.find({segments: {$elemMatch: {isReleased: true}}},...)

You can check the examples on the link for more details.

于 2016-10-29T18:29:30.873 回答