Dit is eigenlijk eenvoudig genoeg, om de resultaten voor elke array op te tellen, is het gewoon een kwestie van onderscheiden tussen welke is en "de elementen combineren". Kortom, u zou dit waarschijnlijk toch in uw documenten moeten doen, zoals blijkt uit de eerste pijplijnfase(n).
Dus voor MongoDB 2.6 en hoger zijn er enkele hulpmethoden:
db.events.aggregate([
{ "$project": {
"app_id": 1,
"event_count": 1,
"all_events": {
"$setUnion": [
{ "$map": {
"input": "$events",
"as": "el",
"in": {
"type": "$$el.type",
"value": "$$el.value",
"class": { "$literal": "A" }
}
}},
{ "$map": {
"input": "$unique_events",
"as": "el",
"in": {
"type": "$$el.type",
"value": "$$el.value",
"class": { "$literal": "B" }
}
}}
]
}
}},
{ "$unwind": "$all_events" },
{ "$group": {
"_id": {
"app_id": "$app_id",
"class": "$all_events.class",
"type": "$all_events.type"
},
"event_count": { "$sum": "$event_count" },
"value": { "$sum": "$all_events.value" }
}},
{ "$group": {
"_id": "$_id.app_id",
"event_count": { "$sum": "$event_count" },
"events": {
"$push": {
"$cond": [
{ "$eq": [ "$_id.class", "A" ] },
{ "type": "$_id.type", "value": "$value" },
false
]
}
},
"unique_events": {
"$push": {
"$cond": [
{ "$eq": [ "$_id.class", "B" ] },
{ "type": "$_id.type", "value": "$value" },
false
]
}
}
}},
{ "$project": {
"event_count": 1,
"events": { "$setDifference": [ "$events", [false] ] },
"unique_events": {
"$setDifference": [ "$unique_events", [false] ]
}
}}
])
Meestal in de $setUnion
en $setDifference
exploitanten. Het andere geval is $map
, die arrays op hun plaats verwerkt. Het hele ding is het doen van bewerkingen op arrays zonder het gebruik van $unwind
. Maar dat kan natuurlijk in eerdere versies, het kost alleen wat meer werk:
db.events.aggregate([
{ "$unwind": "$events" },
{ "$group": {
"_id": "$_id",
"app_id": { "$first": "$app_id" },
"event_count": { "$first": "$event_count" },
"events": {
"$push": {
"type": "$events.type",
"value": "$events.value",
"class": { "$const": "A" }
}
},
"unique_events": { "$first": "$unique_events" }
}},
{ "$unwind": "$unique_events" },
{ "$group": {
"_id": "$_id",
"app_id": { "$first": "$app_id" },
"event_count": { "$first": "$event_count" },
"events": { "$first": "$events" },
"unique_events": {
"$push": {
"type": "$unique_events.type",
"value": "$unique_events.value",
"class": { "$const": "B" }
}
}
}},
{ "$project": {
"app_id": 1,
"event_count": 1,
"events": 1,
"unique_events": 1,
"type": { "$const": [ "A","B" ] }
}},
{ "$unwind": "$type" },
{ "$unwind": "$events" },
{ "$unwind": "$unique_events" },
{ "$group": {
"_id": "$_id",
"app_id": { "$first": "$app_id" },
"event_count": { "$first": "$event_count" },
"all_events": {
"$addToSet": {
"$cond": [
{ "$eq": [ "$events.class", "$type" ] },
{
"type": "$events.type",
"value": "$events.value",
"class": "$events.class"
},
{
"type": "$unique_events.type",
"value": "$unique_events.value",
"class": "$unique_events.class"
}
]
}
}
}},
{ "$unwind": "$all_events" },
{ "$group": {
"_id": {
"app_id": "$app_id",
"class": "$all_events.class",
"type": "$all_events.type"
},
"event_count": { "$sum": "$event_count" },
"value": { "$sum": "$all_events.value" }
}},
{ "$group": {
"_id": "$_id.app_id",
"event_count": { "$sum": "$event_count" },
"events": {
"$push": {
"$cond": [
{ "$eq": [ "$_id.class", "A" ] },
{ "type": "$_id.type", "value": "$value" },
false
]
}
},
"unique_events": {
"$push": {
"$cond": [
{ "$eq": [ "$_id.class", "B" ] },
{ "type": "$_id.type", "value": "$value" },
false
]
}
}
}},
{ "$unwind": "$events" },
{ "$match": { "events": { "$ne": false } } },
{ "$group": {
"_id": "$_id",
"event_count": { "$first": "$event_count" },
"events": { "$push": "$events" },
"unique_events": { "$first": "$unique_events" }
}},
{ "$unwind": "$unique_events" },
{ "$match": { "unique_events": { "$ne": false } } },
{ "$group": {
"_id": "$_id",
"event_count": { "$first": "$event_count" },
"events": { "$first": "$events" },
"unique_events": { "$push": "$unique_events" }
}}
])
Dat levert u de gewenste resultaten op, waarbij elke array wordt "opgeteld" en de master "event_count" met het juiste resultaat.
U zou waarschijnlijk moeten overwegen om beide arrays te combineren met een id die vergelijkbaar is met wat in de pijplijnen is gebruikt, zoals aangetoond. Dit deel is het halve werk. De andere helft overweegt dat u waarschijnlijk vooraf geaggregeerde resultaten ergens in een verzameling moet opslaan voor de beste toepassingsprestaties.