0

I have the following data in the db

  { 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 need to find out the warehouse which has

  • 15 qty of paper
  • 35 qty of postcard

2 Answers 2

1

Try using this find query.

db.warehouse.find(
   {
      $or: [
         {
            item: "paper",
            instock: { $elemMatch: { qty: 15 } }
         },
         {
            item: "postcard",
            instock: { $elemMatch: { qty: 35 } }
         }
      ]
   }
) 
Sign up to request clarification or add additional context in comments.

Comments

0

try this code may help you

db.getCollection('warehouses').aggregate([
  {
    $unwind: "$instock"
  },
  {
    $match: {
      $or: [
        {
          $and: [{ 'item': 'paper' }, { 'instock.qty': { $eq: 15 } }]
        },
        {
          $and: [{ 'item': 'postcard' }, { 'instock.qty': { $eq: 35 } }]
        }
      ]
    }
  }
])

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.