{
"_id" : ObjectId("5b04e9d891081234f8b69199"),
"simpleType" : "somethingSomething",
"embeddedArray" : [
{
"type" : "theTypeIWant",
"data" : [...]
},
{
"type" : "notThatOne",
"data" : [...]
},
{
"type" : "notThatOne",
"data" : [...]
},
{
"type" : "notThatOne",
"data" : [...]
},
{
"type" : "notThatOne",
"data" : [...]
}
]
}
I have a collection which the documents are structured like my sample above. I already started with an aggregation to match the type I want, but I get the whole array with it which I don't want.
db.collection.aggregate(
[
{$project: {"simpleType" : 1, "embeddedArray.type": 1, "embeddedArray.data": 1}},
{$match: {"embeddedArray.type" : "theTypeIWant"}}
]
)
Is there a way to get only that element of the embeddedArray, that matches the type I am searching for, for my projection?
I cannot guarantee that the element with the type I am searching for will always be the first element in my embedded array.
I would like to have a result set like following:
{
"simpleType" : "somethingSomething",
"type": "theTypeIWant",
"data": [...]
}
or:
{
"simpleType" : "somethingSomething",
"embeddedArray":{
"type": "theTypeIWant",
"data": [...]
}
}