If your inventosy document has the following data and you want to get all the warehouses with a value of 'A'
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
Run this query
db.inventory.find( { "instock.warehouse": "A"}, {'instock.warehouse.$': 1} )
The second parameter {'instock.warehouse.$': 1}
is called a projection and I guess it tells mongo to return all matching warehouses with a value of A
Try the above query here