I was checking documentation about how to query an array of embedded documents. They give you this example:
{ 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 } ] }
I want to get only the documents that has qty exactly equals to 5, so I did the following:
db.inventory.find( { 'instock.qty': { $eq: 5 } } )
But it returned:
{ "_id": ObjectId("5e8eda05efe2c455125b60da"), item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ "_id": ObjectId("5e8eda05efe2c455125b60db"), item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ "_id": ObjectId("5e8eda05efe2c455125b60dd"), item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
Why I am getting those documents which clearly doesn't have qty equals to 5, how can I prevent it)? Also, how can I avoid getting the _id
field?