Aggregation
the docs show
db.sales.aggregate(
[
{
$group : {
_id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
]
)
The operation returns the following results:
{ "id" : { "month" : 3, "day" : 15, "year" : 2014 }, "totalPrice" : 50, "averageQuantity" : 10, "count" : 1 }
{ "id" : { "month" : 4, "day" : 4, "year" : 2014 }, "totalPrice" : 200, "averageQuantity" : 15, "count" : 2 }
{ "id" : { "month" : 3, "day" : 1, "year" : 2014 }, "totalPrice" : 40, "averageQuantity" : 1.5, "count" : 2 }
Group by null
Yet when I do the following
// Requires official MongoShell 3.6+
// Requires official MongoShell 3.6+
use pt2018;
db.getCollection("GymData").aggregate(
[
{
"$sort" : {
"Tenant" : 1.0
}
},
{
"$group" : {
"id" : {
"tenant" : "$Tenant",
"month" : {
"$month" : "$MessageLocalDateTime"
},
"year" : {
"$year" : "$MessageLocalDateTime"
}
},
"count" : {
"$sum" : 1.0
}
}
}
],
{
"allowDiskUse" : false
}
);
I get _id and count colums
with the bility to click and show the 3 fields in _id but if I show the 3 fields it does not show count. Would like to display all fields with total just like the mongo docs show.