You can add another group pipeline in which you specify the value _id None to calculate the accumulated values ββfor all input documents as a whole, here you can get the total score, as well as the original group is considered, although in the accumulated array:
>>> pipeline = [ ... {"$match": options}, ... {"$group": {"_id": groupby, "count": {"$sum":1}}}, ... {"$group": {"_id": None, "total": {"$sum": 1}, "details":{"$push":{"groupby": "$_id", "count": "$count"}}}} ... ] >>> list(db.collection.aggregate(pipeline))
chridam
source share