I would like mongodb to only return a matching item from an array. For some reason it returns the complete document and not just the item.
My data :
{
"car_type": "fiat",
"car_parts": [
{"part_name": "door", "part_color": "blue"},
{"part_name": "roof", "part_color": "purple"}
]
},
{
"car_type": "ferrari",
"car_parts": [
{"part_name": "door", "part_color": "red"},
{"part_name": "wheels", "part_color": "yellow"}
]
},
If I do
collection.find_one(filter={"car_type": "fiat", "car_parts": {"$elemMatch": {"part_color": "blue"}}})
I get the complete document :
{
"car_type": "fiat",
"car_parts": [
{"part_name": "door", "part_color": "blue"},
{"part_name": "roof", "part_color": "purple"}
]
}
What I would like instead is the matching item from the array
{"part_name": "door", "part_color": "blue"}
There is plenty documentation about matching an item from an array but none about getting only the matching item instead of getting the whole doc. I really don't want to get all of the array's content. I just need the matching item, that's all...
Thx