我有两个收藏:
带有一些操作的集合:
[
{
"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
}
]
将感激的帮助!
1条答案
按热度按时间jchrr9hc1#
一个选项是使用
$group
两次,然后使用$reduce
来展平数组:了解它在playground example上的工作原理