1

I am quite a newbie to mongodb, my requirement is to filter within the array of objects of a single document

for example : Below is my json document. I want to query in the combinations to find all the Manufacturer with value "abc manufacturer".

The query I tried is db.Product.find({"Combinations": {$elemMatch: {"Manufacturer":"abc manufacturer"}}}). unfortunately its not returning the result with abc manufacturer but all.

My result should be exactly similar to the screenshot attached down below. Since its in sql server, Now I want the equivalent query for the same in mongodb.. Some experts in the forum throw me some light.

Screenshot

{ "_id" : ObjectId("59e8c938ab3166800493273f"), "ProductId" : 26, "Combinations" : [ { "#Type" : "S", "Manufacturer" : "abc manufacturer", "Model Name" : "Squatting Urinal", "Size" : "475 x 365 x 105 mm", "Colour" : "White" }, { "#Type" : "S", "Manufacturer" : "abc manufacturer", "Model Name" : "Squatting", "Size" : "430 x 350 x 100 mm" }, { "#Type" : "S", "Manufacturer" : "def manufacturer", "Model Name" : "Squatting Urinal", "Size" : "440 x 355 x 102 mm", "Colour" : "White" }, { "#Type" : "S", "Manufacturer" : "xyz manufacturer", "Model Name" : "Squatting Urinal", "Size" : "440 x 355 x 102 mm", "Colour" : "Ivory" }, { "#Type" : "S", "Manufacturer" : "ghi manufacturer", "Model Name" : "Squatting Pan - 861" }, { "#Type" : "S", "Manufacturer" : "xyz manufacturer", "Model Name" : "Mateo", "Size" : "470 x 365 x 100 mm" }, { "#Type" : "S", "Manufacturer" : "xyz manufacturer", "Model Name" : "Squatting", "Size" : "340 x 435 x 100 mm", "Colour" : "White" } ] }

Jebin
  • 163
  • 2
  • 11

1 Answers1

0

If I understand what you are searching for, is all the Combinations where the Manufacturer is abc. The query you just did gives you all the Documents that contains a Combination with the said Manufacturer.

You should check the aggregations for that.

https://docs.mongodb.com/manual/aggregation/

D. Saby
  • 61
  • 2
  • 6