My goal is to:
- Get all the documents with
status=true. - And return only objects with
active=truein thelifearray.
Below is what my MongoDB documents look like:
{
"name": "justine",
"life" : [
{
"status" : true,
"service" : [
{
"partner" : "pat 1",
"active" : true,
},
{
"partner" : "pat 2",
"active" : false
}
}
]
},
{
"name": "okumu",
"life" : [
{
"status" : true,
"service" : [
{
"partner" : "pat 1",
"active" : true,
},
{
"partner" : "pat 2",
"active" : true
}
}
]
}
Expected output:
{
"name": "justine",
"life" : [
{
"status" : true,
"service" : [
{
"partner" : "pat 1",
"active" : true,
}
}
]
},
{
"name": "okumu",
"life" : [
{
"status" : true,
"service" : [
{
"partner" : "pat 1",
"active" : true,
},
{
"partner" : "pat 2",
"active" : true
}
}
]
}
This is what I did:
await Users.find({ life: { $elemMatch: { status: true, life: { $elemMatch: { active: false } } } }});
This is working well for the first condition, in case the second condition is not met, the entire object is not returned, however, if it's met, even the active=false objects are returned.
I'll be grateful if you can help me out, am not a MongoDB expert.