I have the following type of document where I have to find instock elements based on a given value. i.e Return all instocks where ele = 5.
{"item":"journal",
"instock":[
{ "warehouse":"A", "ele":[2,4,5] },
{ "warehouse":"C", "ele":[8,5,2] },
{ "warehouse":"F", "ele":[3] },
{ "warehouse":"K", "ele":[2,8,4] }
]
}
I tried to use $elemMatch but it just produces the first element.
db.inventory.find({"item": "journal"}, {"_id": 0, "item": 0, "instock":{$elemMatch: {"ele": {$in: [5]}}} })
But it just gives:
{ "instock" : [
{ "warehouse" : "A", "ele" : [ 2, 4, 5 ] }
]}
And the expectation is
{ "instock" : [
{ "warehouse" : "A", "ele" : [ 2, 4, 5 ] },
{ "warehouse" : "C", "ele" : [ 8, 5, 2 ] }
]}
How should I get the expected result?