cExample Mongo Document
{
"_id": ObjectId("5652e77f21b0f1f2692558a1"),
"category": "clothing",
"Brand": [
{
"name": "Adidas",
"key": "Adidas"
},
{
"name": "Reebok",
"key": "Reebok"
},
{
"name": "Puma",
"key": "Puma"
}
],
"Color": [
{
"name": "Red",
"key": "Red"
},
{
"name": "Green",
"key": "Green"
},
{
"name": "Blue",
"key": "Blue"
}
]
}
Now I want to search for Brands where name is either Adidas or Puma. Other requerement is to return sub document only so I tried query given below
find( { "Brand.name" : { $in : ["Reebok", "Adidas"]}},{"Brand" : 1, "_id" : 0})
But its returning me all three objects of Brand array
{
"Brand" : [
{
"name" : "Adidas",
"key" : "Adidas"
},
{
"name" : "Reebok",
"key" : "Reebok"
},
{
"name" : "Puma",
"key" : "Puma"
}
]
}
I can sertainly do it through Javascript as well (Iterating through retuned array and metching values) but Is there any way to get my purpose solved with Mongo Query only.
Note : In NoSql Systems, where JSON documents are supported, biggest difference is these systems doesn't support relationships but you can always maintain relations by subdocuments. If such queries (mentioned above) are not possible then I think holding data in subdocument form is not good practise as you can not query it as you want. Expert Thoughts on this????