我得到了带有以下示例数据的以下模型reportcharities
:
/* 1 */
{
"_id" : ObjectId("5e1d79b93b591243962fa0c5"),
"userId" : ObjectId("5dee2e536c3a72543518a0fa"),
"charityId" : ObjectId("5dce381401a0171c50989d65"),
"createdAt" : ISODate("2020-01-14T08:20:09.965Z"),
"updatedAt" : ISODate("2020-01-14T08:20:09.965Z"),
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("5e1d7afd3b591243962fa0c6"),
"userId" : ObjectId("5dee2e536c3a72543518a0fa"),
"eventId" : ObjectId("5df1ea1efe164a3c9ef854df"),
"createdAt" : ISODate("2020-01-14T08:25:33.169Z"),
"updatedAt" : ISODate("2020-01-14T08:25:33.169Z"),
"__v" : 0
}
/* 3 */
{
"_id" : ObjectId("5e1d7b2c3b591243962fa0c7"),
"userId" : ObjectId("5dee2e536c3a72543518a0fa"),
"projectId" : ObjectId("5df73c88bc67dc3b60a542fc"),
"createdAt" : ISODate("2020-01-14T08:26:20.551Z"),
"updatedAt" : ISODate("2020-01-14T08:26:20.551Z"),
"__v" : 0
}
/* 4 */
{
"_id" : ObjectId("5e1dad44121cc1481303fb4d"),
"userId" : ObjectId("5e1d8c4d3b591243962fa0d0"),
"eventId" : ObjectId("5e1d8bdd3b591243962fa0cf"),
"createdAt" : ISODate("2020-01-14T12:00:04.231Z"),
"updatedAt" : ISODate("2020-01-14T12:00:04.231Z"),
"__v" : 0
}
/* 5 */
{
"_id" : ObjectId("5e1dad7c121cc1481303fb4e"),
"userId" : ObjectId("5e1d8c4d3b591243962fa0d0"),
"eventId" : ObjectId("5e1d8bdd3b591243962fa0cf"),
"createdAt" : ISODate("2020-01-14T12:01:00.322Z"),
"updatedAt" : ISODate("2020-01-14T12:01:00.322Z"),
"__v" : 0
}
/* 6 */
{
"_id" : ObjectId("5e1dad8b121cc1481303fb4f"),
"userId" : ObjectId("5e1d8c4d3b591243962fa0d0"),
"eventId" : ObjectId("5e1d8bdd3b591243962fa0cf"),
"createdAt" : ISODate("2020-01-14T12:01:15.181Z"),
"updatedAt" : ISODate("2020-01-14T12:01:15.181Z"),
"__v" : 0
}
/* 7 */
{
"_id" : ObjectId("5e1dada9121cc1481303fb50"),
"userId" : ObjectId("5e1d8c4d3b591243962fa0d0"),
"eventId" : ObjectId("5e1d8bdd3b591243962fa0cf"),
"createdAt" : ISODate("2020-01-14T12:01:45.433Z"),
"updatedAt" : ISODate("2020-01-14T12:01:45.433Z"),
"__v" : 0
}
/* 8 */
{
"_id" : ObjectId("5e1eb08ecaa8076b22547abc"),
"userId" : ObjectId("5e1e9d5744b39654f05ed8b4"),
"eventId" : ObjectId("5e1d8bdd3b591243962fa0cf"),
"createdAt" : ISODate("2020-01-15T06:26:22.999Z"),
"updatedAt" : ISODate("2020-01-15T06:26:22.999Z"),
"__v" : 0
}
在这里,作为
userId
的用户可以一次将慈善组织报告为charityId
,将项目报告为projectId
或将事件报告为eventId
。现在,我需要按组查找每个类别的计数,并将其计数作为用户的总报告数。例如,对于这些“类型”中的每一个,我想得到以下计数:
"eventId" : ObjectId("5e1d8bdd3b591243962fa0cf"), count: 5
"projectId" : ObjectId("5df73c88bc67dc3b60a542fc"), count: 1
"charityId" : ObjectId("5dce381401a0171c50989d65") count: 1
"eventId" : ObjectId("5df1ea1efe164a3c9ef854df") count: 1
...等等
文档中仅存在
charityId
,projectId
或eventId
之一,我无法在聚合中实现多个分组。 最佳答案
您需要使用以下聚合来重新构建数据:
db.collection.aggregate([
{
"$addFields": {
"data": {
"$filter": {
"input": {
"$objectToArray": "$$ROOT"
},
"as": "datum",
"cond": {
"$in": [
"$$datum.k",
[
"eventId",
"projectId",
"charityId"
]
]
}
}
}
}
},
{
"$unwind": "$data"
},
{
"$group": {
"_id": {
"key": "$data.k",
"value": "$data.v"
},
"count": {
"$sum": 1.0
}
}
},
{
"$project": {
"type": "$_id.key",
"count": "$count",
"_id": "$_id.value"
}
}
]);
将为您提供以下输出:
{
"type" : "projectId",
"count" : 1.0,
"_id" : ObjectId("5df73c88bc67dc3b60a542fc")
}
{
"type" : "eventId",
"count" : 1.0,
"_id" : ObjectId("5df1ea1efe164a3c9ef854df")
}
{
"type" : "charityId",
"count" : 1.0,
"_id" : ObjectId("5dce381401a0171c50989d65")
}
{
"type" : "eventId",
"count" : 5.0,
"_id" : ObjectId("5e1d8bdd3b591243962fa0cf")
}
关于node.js - 根据 key 是否存在进行多个分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59878008/