Home > Enterprise >  Not able to filter out the arrrays of object in mongodb
Not able to filter out the arrrays of object in mongodb

Time:01-11

    "_id" : ObjectId("61dbab7f0beb8c8075370217"),
    "name" : "Question 2",
    "answers" : [
        {
            "text" : "something 1",
            "isDeleted" : false
        },
        {
            "text" : "something 2",
            "isDeleted" : true
        },
        {
            "text" : "something 2",
            "isDeleted" : false
        }
    ]
}

I have this kind of document where I want to find all answers objects which have isDeleted should be false

CodePudding user response:

You can use $filter

db.collection.find({},
{
  answers: {
    $filter: {
      input: "$answers",
      cond: {
        $eq: [
          "$$this.isDeleted",
          false
        ]
      }
    }
  },
  name :1
})

Working Mongo playground

  •  Tags:  
  • Related