Suppose you have the following:
// Document 1
{ "shapes" : [
{"shape" : "square", "color" : "blue"},
{"shape" : "circle","color" : "red"}
]
}
// Document 2
{ "shapes" : [
{"shape" : "square", "color" : "black"},
{"shape" : "circle", "color" : "green"}
]
}
do query:
db.test.find({"shapes.color":"red"}, {"shapes.color":1})
or
db.test.find({shapes: {"$elemMatch": {color: "red"}}}, {"shapes.color":1})
return document (Document 1) matched, but with always ALL array items in shapes:
{ "shapes" : [
{"shape" : "square", "color" : "blue"},
{"shape" : "circle","color" : "red"}
]
}
I just want document (Document 1) filtered with ONLY array item color=red:
{ "shapes" : [
{"shape" : "circle","color" : "red"}
]
}
How ?