I need to group these docs by taxonomy array but I've to exclude from group the "term3" item from the first Doc. (My situation is more complicated in a real app but this example fits.)
[
{
"_id": "1",
"Taxonomy": [
{
"Key": "1", "Term": "term1"
},
{
"Key": "2", "Term": "term2"
},
{
"Key": "3", "Term": "term3"
}
]
},
{
"_id": "2",
"Taxonomy": [
{
"Key": "1", "Term": "term1"
},
{
"Key": "2", "Term": "term2"
}
]
},
{
"_id": "3",
"Taxonomy": [
{
"Key": "1", "Term": "term1"
},
{
"Key": "2", "Term": "term2"
}
]
}
]
This command generates two nodes due to the "term3" item:
{$group: {"_id" :"$Taxonomy","Posts" : { "$addToSet" : { "_id" : "$_id"}}}}
It is possible to unwind, match and then re-group documents or there is a simple way?
limit, I need almost tomatch... but I'm stuck with the syntax!