Hi ,
Here is my data model
[
{
"_id": 1,
"racks": [
{
"rackId": 1,
"available": true
},
{
"rackId": 2,
"available": true
},
{
"rackId": 3,
"available": true
},
{
"rackId": 4,
"available": false
},
{
"rackId": 5,
"available": false
},
{
"rackId": 6,
"available": false
},
]
},
{
"_id": 2,
"racks": [
{
"rackId": 1,
"available": true
},
{
"rackId": 2,
"available": true
},
{
"rackId": 3,
"available": true
}
]
},
{
"_id": 3,
"racks": [
{
"rackId": 1,
"available": true
},
{
"rackId": 2,
"available": true
},
{
"rackId": 3,
"available": true
}
]
}
]
I want to find if a document matches the conditions provided.
a. Accept a list. Where list of “rackId” are given
b. For each of rackId mentioned in list, its “available” value should be considered. In this case it should be true.
Note i have to use $all as i might have many rackId’s to query so that i do not end up writing as many rack objects in the query.
Example if i need to match 1 to 6 rackId with “available” true , i can pass [1, 2, 3, 4, 5, 6] using $all.
My query so far. Which provides no documents found even though document “_id” 1 has rackId 1, rackId 2 and racked 3 with available field as “true”. Could you please help me in getting correct query considering $all operation ?
a. Mongo query
b. Corresponding java spring query equivalent ?
db.collection.find({
"_id": 1,
"racks": {
"$elemMatch": {
"rackId": {
$all: [
1,
2,
3
]
},
"available": true
}
}
})
I did researched similar topic on this thread How to match $all and equal to conditions in $elemMatch operator? but not satisfied with the approach as it is not using $all