我有一个集合,该集合捕获有关用户在阅读以下内容的页面上停留了多长时间的数据:
{
"_id": ObjectId("5be2098b19ee5c85c9f2d01d"),
"user": ObjectId("5be208de25f1d385a47c80c2"),
"sessionID": "iwVCwciXNeqs0zucHzyMNvl5RCepTUtq",
"report": ObjectId("5bdb09d2dcd6ae0fcbbb5f92"),
"timeReading": 86.371,
"timestamp": ISODate("2018-11-06T21:37:15.123Z"),
}
,而我试图获得a)所有文档的总阅读时间,b)每个用户的总阅读时间。我的目标是以下输出:
{
totalTime: 138.6,
users: [{
user: ObjectId("5be208de25f1d38few7c80c2"),
timeReading: 75.3
},
{
user: ObjectId("5be208de25f1d385a47c2355"),
timeReading: 63.3
}]
}
到目前为止,我已经得到:
Usage.aggregate([
{
"$group": {
_id: {
user: "$user"
},
count: {
$sum: "$timeReading"
},
}
},...
这给了我
[ { _id: { user: 5bad2bafea7fab84fafc896d }, count: 75.345 },
{ _id: { user: 5be208de25f1d385a47c80c2 }, count: 6.371 } ]
但是我似乎无法找出如何在同一查询中获得总体总数。