我正在使用以下汇总查询从我的集合中获取字段数。
Worksheet.aggregate([
{
"$group": {
"_id": null,
"todayBilling": {
"$sum": {
"$cond": [{ "$and" : [ {"$eq": [ "$isBilling", true]}, {$eq: [ "$date",new Date(moment().format('l'))] } ]},"$hours",0]
}
},
"todayProjects":{
"$sum": {
"$cond": [{ $eq: [ "$date",new Date(moment().format('l'))] },1,0]
}
}
}
}
])
下面是我的工作表集合:
/* 1 */
{
"_id" : ObjectId("5c34d84926471a1ce0d3103c"),
"isBilling" : true,
"isActive" : true,
"reportSent" : false,
"projectId" : ObjectId("5c34d81d26471a1ce0d3103b"),
"hours" : 6,
"date" : ISODate("2019-01-07T12:00:00.000-06:30"),
}
/* 2 */
{
"_id" : ObjectId("5c34d8d226471a1ce0d3103e"),
"isBilling" : true,
"isActive" : false,
"reportSent" : false,
"projectId" : ObjectId("5c34d81d26471a1ce0d3103b"),
"hours" : 3,
"date" : ISODate("2019-01-07T12:00:00.000-06:30"),
}
现在,我在两个文档中都有相同的projectId,并且todayProjects
给了我 2 ,但是如果项目ID与以前的项目ID相同,我不想累加。
下面是我想要的输出:
{
todayBilling: 6(it is calculating current date vise)
todayProjects: 3(it should be calculate current date vise)
}