My first query returns the following result, after various aggregation pipeline stages:
{
"group" : "A",
"count" : 6,
"total" : 20
},
{
"group" : "B",
"count" : 2,
"total" : 50
}
My second query returns the following result, after various aggregation pipeline stages:
{
"group": "A",
"count": 4,
"total": 80
},
{
"group": "C",
"count": 12,
"total": 60
}
Both the queries are performed on the same collection, but groups and transforms the data differently based on the pipeline stages.
Both of my queries use different
$matchconditions, have various pipeline stages including$facet,$unwind,$group,$projectand operators like$map,$reduce,$zip,$subtract...
db.collection.aggregate([
{ $unwind...},
{ $match....},
{ $facet...},
...
])
When I use $facet to run my queries as parallel queries, it gives the following error (because I'm already using $facet in my existing queries) :
$facet is not allowed to be used within a $facet stage
Expected Output:
I need to find the average value for each of the group.
For that, I need to combine the results of both the queries and perform queries on the combined result.
My combined stage should look like this:
{
"group" : "A",
"count" : 10,
"total" : 100
},
{
"group" : "B",
"count" : 2,
"total" : 50
},
{
"group": "C",
"count": 12,
"total": 60
}
Expected final result with average value for each group:
{
"group" : "A",
"avg" : 10
},
{
"group" : "B",
"avg" : 25
},
{
"group": "C",
"avg": 5
}
Are there any operators available in MongoDB aggregation pipeline to achieve this without modifying my existing queries?
How to achieve this use case?
Thanks!