0

Schema:

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

and i have data likewise:

_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,
        },
    ]

i wants list of objects with status: true and Iscompleted: 0 I am using following query but cant able to get only require data..

Model.find({ _id: "5956020e4f605403d85f24d5" },
                 { Actionusers: { $elemMatch: { Iscompleted: 1, status: true } } }).select({Actionusers:1})
SGR Dalal
  • 121
  • 13
  • `$elemMatch` and standard projection match **"one"** item only. [See the answers to the existing question](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) containing `$filter` for "multiple" matches. – Neil Lunn Jul 03 '17 at 08:12
  • HI @Neil, I just used $filter but no luck to get desired data...can you please help me out ?`.aggregate([ { $match: { 'Actionusers.Iscompleted': 0 } }, { $project: { Actionusers: { $filter: { input: '$Actionusers', as: 'shape', cond: { $eq: ['$$shape.Iscompleted', 0] } } }, _id: '5956020e4f605403d85f24d5' } } ])` – SGR Dalal Jul 03 '17 at 08:20
  • You need [`$and`](https://docs.mongodb.com/manual/reference/operator/aggregation/and/) for the two conditions. `cond: { $and: [ { $eq: ['$$shape.Iscompleted', 0] }, '$$shape.status' ] }` – Neil Lunn Jul 03 '17 at 08:23
  • Hey man, still its returning to me whole array. what i am doing wrong ? – SGR Dalal Jul 03 '17 at 08:31

0 Answers0