2

I am new to MongoDB. I am trying to get field values for a particular sku from the array in the document. I tried query ({"items.sku":"abc"},{"items.price":1}) but it is giving prices for both skus. I would appreciate if anybody can tell me how I can get price for only sku:"abc" i.e 3.5?

{  "_id": 123,
     "store": "store1",
     "items": [ { sku: "abc", qty: 4, price: 3.5 },
              { sku: "def", qty: 3, price: 2.5 } ]
}
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Divya
  • 43
  • 1
  • 4

1 Answers1

9

Please try:

db.getCollection('abhey').find({"items.sku":"abc"},{_id: 0, 'items.price.$': 1})

In case you need more help, please feel free to write.

Abie
  • 624
  • 5
  • 12