Hi everyone - got a bit of a question
Collection data -
{
"name": "name 1",
"type": 50,
"qty": 1
},
{
"name": "name 2",
"type": 52,
"qty": 2
},
{
"name": "name 3",
"type": 50,
"qty": 3
},
{
"name": "name 4",
"type": 52,
"qty": 4
},
{
"name": "name 5",
"type": 50,
"qty": 5
}
and have 2 queries - sum and count
db.example.aggregate(
{
$match: {"type": 50}
},
{
$group: {
_id: null,
total: { $sum: "$qty" },
count: { $sum: 1 }
}
},
{
$project: {"_id": 0, total: 1, count: 1}
}
)
and detail matching on some criteria
db.example.aggregate(
{
$match: {"type": 50}
}
)
Is there any way of combining the 2 queries together so the sum and count and details appear together in the output? like so
{ "total" : 9, "count" : 3 }
{ "_id" : ObjectId("5eea8e09279c3208b5132694"), "name" : "name 1", "type" : 50, "qty" : 1 }
{ "_id" : ObjectId("5eea8e09279c3208b5132696"), "name" : "name 3", "type" : 50, "qty" : 3 }
{ "_id" : ObjectId("5eea8e09279c3208b5132698"), "name" : "name 5", "type" : 50, "qty" : 5 }
thanks
Steve