Not able to filter out the array of object in mongodb
{
"_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
is false
.
You can use $filter
db.collection.find({},
{
answers: {
$filter: {
input: "$answers",
cond: {
$eq: [
"$$this.isDeleted",
false
]
}
}
},
name :1
})
Working Mongo playground