2

I have a list of objects something like

[
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15, type : ["hello", "world", "wassup", "yo"] } ] },
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 1515, type : ["hello", "wassup", "yo"] } ] },
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 515, type : ["hello"] } ] },
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 3515, type : ["wassup", "yo"] } ] }
]

How can I query list of all warehouse in which "type" contains hello

db.inventory.find( { "instock": { $elemMatch: {type :["hello"]}}} , {_id:0})

returns the objects that has only hello.

1 Answer 1

2

Dot notation works in this case, try:

db.collection.find({ "instock.type": "hello"})

MongoDB playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.