I have these type of data on a Collection:
{
"_id": {
"$oid": "6014662b3a38ba4929b602ab"
},
"collection": [
{
"name": "Facility",
"_id": {
"$oid": "601466293a38ba4929b602aa"
},
"is_deleted": false
},
{
"name": "User",
"_id": {
"$oid": "60021529c02fa2b27e7c1989"
},
"is_deleted": false
},
{
"name": "Asset",
"_id": {
"$oid": "6018948fc35a109224439f73"
},
},
{
"name": "Asset",
"_id": {
"$oid": "604a734fca10c74d800031db"
},
"is_deleted": true
}
],
"customer": "TestCustomer",
"is_deleted": false
}
And also I have this query
db.getCollection("Collection").aggregate([{
'$match': {
'$and': [{
'collection.name': 'User',
'collection._id': ObjectId('60021529c02fa2b27e7c1989'),
'collection.is_deleted': false
}, {
'collection.name': 'Asset',
'collection._id': ObjectId('604a734fca10c74d800031db'),
'collection.is_deleted': false
}],
'customer': 'TestCustomer',
'is_deleted': false
}
}])
is_deleted on Asset is true in the database, but I am requesting when the value is false.
When this query is executed, is not supposed to return nay data, but it does. I have been trying to resolve this issue but nothing has helped so far.
Because you're trying to match elements inside an array, you should use $elemMatch instead. Also there is no need for the $and operator.
db.collection.aggregate([
{
"$match": {
"collection": {
"$elemMatch": {
"name": "User",
"_id": ObjectId("60021529c02fa2b27e7c1989"),
"is_deleted": false
}
},
"collection": {
"$elemMatch": {
"name": "Asset",
"_id": ObjectId("604a734fca10c74d800031db"),
"is_deleted": false
}
},
"customer": "TestCustomer",
"is_deleted": false
}
}
])
Playground: https://mongoplayground.net/p/cdz9TgQ8HNw