我有两个Collection :

具有一些操作的集合:

[
  {
    "id": 0,
    "action": "qwe",
    "timestamp": "2022-11-20T23:59:59",
    "user": "test"
  },
  {
    "id": 1,
    "action": "zwe",
    "timestamp": "2022-11-20T11:22:33",
    "user": "test"
  },
  {
    "id": 1,
    "action": "qwe",
    "timestamp": "2022-11-20T11:22:33",
    "user": "test"
  }
]

并收集了这些动作的分数:

[
  {
    "action": "qwe",
    "score": 5
  },
  {
    "action": "zwe",
    "score": 3
  },
  {
    "action": "qwe",
    "score": 5
  }
]

我需要取得下一步的成果:

[
  {
    "user": "test",
    "actions": [
      {
        "action":"qwe",
        "score":5,
        "timestamp":"2022-11-17T12:55:34.804+00:00"
      },
      {
        "action":"zwe",
        "score":3,
        "timestamp":"2022-11-17T12:55:34.804+00:00"
      },
      {
        "action":"qwe",
        "score":5,
        "timestamp":"2022-11-18T12:51:11.804+00:00"
      }
    ],
    "actions_total": [
      {
          "action": "qwe",
          "count": 2,
          "total_score": 10
      },
      {
          "action": "zwe",
          "count": 1,
          "total_score": 3
      }
    ],
    "total_score": 13
  }
]

我写下了《下一个聚合》:

[
  {
    $match: {
      timestamp: {
        $gte: ISODate(
        '2022-11-17T00:00:00'
        ),
        $lte: ISODate(
        '2022-11-20T23:59:59'
        )
      }
    }
  },
  {
    $lookup: {
      from: 'actions_score',
      localField: 'action',
      foreignField: 'action',
      as: 'score'
    }
  },
  {
    $unwind: {
      path: "$score"
    }
  },
  {
    $project: {
      'username': true,
      'action': true,
      'score': '$score.score',
      'timestamp': true,
      'role_id': true,
      '_id': false
    }
  },
  {
    $group: {
      _id: '$username',
      'username': {
        $first: '$username'
      },
      'role_id': {
        $first: '$role_id'
      },
      'actions': {
        $addToSet: '$$ROOT'
      }
    }
  },
  {
    $addFields: {
      'total_score': {
        "$sum": "$actions.score"
      }
    }
  },
  {
    $project: {
      _id: false,
      'actions.username': false,
      'actions.role_id': false
    }
  }
]

我得到了下一个结果:

[
  {
    "user": "test",
    "actions": [
      {
        "action":"qwe",
        "score":5,
        "timestamp":"2022-11-17T12:55:34.804+00:00"
      },
      {
        "action":"zwe",
        "score":3,
        "timestamp":"2022-11-17T12:55:34.804+00:00"
      },
      {
        "action":"qwe",
        "score":5,
        "timestamp":"2022-11-18T12:51:11.804+00:00"
      }
    ],
    "total_score": 13
  }
]

问题:

我怎么才能得到这个角色?如何通过现场操作摸索文档并获得预期结果?

"actions_total": [
      {
          "action": "qwe",
          "count": 2,
          "total_score": 10
      },
      {
          "action": "zwe",
          "count": 1,
          "total_score": 3
      }
    ]

一定会感激您的帮助的!

推荐答案

一种 Select 是使用$group键两次,然后使用$reduce键展平数组:

db.actions.aggregate([
  {$match: {timestamp: {
        $gte: ISODate("2022-11-17T00:00:00Z"),
        $lte: ISODate("2022-11-20T23:59:59Z")
  }}},
  {$lookup: {
      from: "actions_score",
      localField: "action",
      foreignField: "action",
      as: "score"
  }},
  {$project: {
      user: 1, _id: 0, action: 1, score: {$first: "$score.score"}, timestamp: 1}},
  {$group: {
      _id: {user: "$user", action: "$action"},
      count: {$sum: 1},
      total_score: {$sum: "$score"},
      actions: {$addToSet: "$$ROOT"}
  }},
  {$group: {
      _id: "$_id.user",
      user: {$first: "$_id.user"},
      actions: {$push: "$actions"},
      actions_total: {$push: {
          action: "$_id.action",
          count: "$count",
          total_score: "$total_score"
      }},
      total_score: {$sum: "$total_score"}
  }},
  {$set: {
      actions: {$reduce: {
          input: "$actions",
          initialValue: [],
          in: {$concatArrays: ["$$value", "$$this"]}
      }}
  }}
])

看看它在playground example号公路上是如何工作的

Mongodb相关问答推荐

使用单个/多个值查询任何/特定,而不使用if块

MongoDB聚合匹配字符串字符

如何在Mongo Aggregate Query中创建集合的对象ID数组,并在列表中查找另一个集合中的ID

查找/查找单个深度嵌套文档的多个集合

MongoDB$unionWith,如何 Select 特定文档

数组相等条件返回的文档多于与相等匹配的文档:MongoDB

如何在Mongo中制作全覆盖索引

所有文档中嵌套 struct 中的条件匹配-mongodb

在数组对象 Mongodb 中仅 Select 需要的数组

Mongoose 聚合和多级组

如何获取mongodb ObjectId的值?

@DynamicPropertySource 未被调用(Kotlin、Spring Boot 和 TestContainers)

mongodb - 查找包含至少 3 个填充数组的记录

MongoDB查询仅返回嵌入文档

使用模拟 MongoDB 服务器进行单元测试

无法连接到mongolab主机

MongoDB:设置 Windows 服务

MongoDB的数据库管理工具

是否有支持 MongoDB 和 Devise 的 Rails 管理界面?

MongoDB Compass:select distinct field values