如何仅从MongoDb中的数组中获取过滤对象

时间:2017-07-03 08:09:31

标签: mongodb mongoose mongodb-query mongoose-schema

架构:

var instaSchema = mongoose.Schema({
    Active: {
        type: Number,
        default: 1
    },
    Actionusers: [{
        Actiontype: {
            type: Number,
        },
        Iscompleted: {
            type: Number,
            default: 0,
        },
        status: {
            type: Boolean,
            default: false,
        },
    }]
})

我同样有数据:

_id: 5956020e4f605403d85f24d5,
    Active: 1,
    Actionusers: [{
            Actiontype: 1,
            _id: 5957420f5decd01de09deb3a,
            status: true,
            Iscompleted: 1,
        },

        {
            Actiontype: 1,
            _id: 5957420f5decd01de09deb3f,
            status: true,
            Iscompleted: 1,
        },


        {
            Actiontype: 1,
            _id: 5957420f5decd01de09deb44,
            status: true,
            Iscompleted: 0,
        },


        {
            Actiontype: 1,
            _id: 5957420f5decd01de09deb49,
            status: true,
            Iscompleted: 0,
        },


        {
            Actiontype: 1,
            _id: 5957420f5decd01de09deb4e,
            status: true,
            Iscompleted: 0,
        },
    ]

我想要status: trueIscompleted: 0的对象列表 我正在使用以下查询,但无法获得只需要数据..

Model.find({ _id: "5956020e4f605403d85f24d5" },
                 { Actionusers: { $elemMatch: { Iscompleted: 1, status: true } } }).select({Actionusers:1})

0 个答案:

没有答案