一直试图阅读文档和示例来尝试解决这个问题,但事实证明这很困难(尤其是作为一个不太了解所有不同术语背后含义的noobie )。
我们有几个集合,并试图量化/图表其中的一些数据。我们设法通过聚合阶段对数据进行按摩,使文档看起来如下所示:
[
{
_id: 1,
sender: 'foo',
messages: [
{_id: 10, text: 'hello', recipientCount: 3, sentAt: '2019-10'},
{_id: 11, text: 'hello', recipientCount: 3, sentAt: '2019-10'},
{_id: 12, text: 'hello', recipientCount: 3, sentAt: '2019-10'},
]
},
{
_id: 2,
sender: 'bar',
messages: [
{_id: 13, text: 'hello', recipientCount: 3, sentAt: '2018-10'},
{_id: 14, text: 'hello', recipientCount: 3, sentAt: '2018-10'},
{_id: 15, text: 'hello', recipientCount: 3, sentAt: '2018-10'},
]
},
{
_id: 3,
sender: 'foo',
messages: [
{_id: 16, text: 'hello', recipientCount: 13, sentAt: '2020-10'},
{_id: 17, text: 'hello', recipientCount: 13, sentAt: '2020-10'},
{_id: 18, text: 'hello', recipientCount: 13, sentAt: '2020-10'},
]
},
{
_id: 4,
sender: 'foo',
messages: [
{_id: 19, text: 'hello', recipientCount: 3, sentAt: '2021-10'},
{_id: 110, text: 'hello', recipientCount: 3, sentAt: '2021-10'},
{_id: 111, text: 'hello', recipientCount: 3, sentAt: '2021-10'},
]
},
{
_id: 5,
sender: 'bar',
messages: [
{_id: 112, text: 'hello', recipientCount: 1, sentAt: '2021-4'},
{_id: 113, text: 'hello', recipientCount: 1, sentAt: '2021-4'},
{_id: 114, text: 'hello', recipientCount: 1, sentAt: '2021-4'},
]
},
{
_id: 6,
sender: 'foo',
messages: [
{_id: 115, text: 'hello', recipientCount: 4, sentAt: '2020-8'},
{_id: 116, text: 'hello', recipientCount: 4, sentAt: '2020-8'},
{_id: 117, text: 'hello', recipientCount: 4, sentAt: '2020-8'},
]
},
{
_id: 7,
sender: 'cap',
messages: [
{_id: 118, text: 'hello', recipientCount: 7, sentAt: '2018-6'},
{_id: 119, text: 'hello', recipientCount: 7, sentAt: '2018-6'},
{_id: 120, text: 'hello', recipientCount: 7, sentAt: '2018-6'},
]
},
{
_id: 8,
sender: 'cap',
messages: [
{_id: 121, text: 'hello', recipientCount: 12, sentAt: '2019-11'},
{_id: 122, text: 'hello', recipientCount: 12, sentAt: '2019-11'},
{_id: 123, text: 'hello', recipientCount: 12, sentAt: '2019-11'},
]
},
{
_id: 9,
sender: 'foo',
messages: [
{_id: 124, text: 'hello', recipientCount: 2, sentAt: '2020-12'},
{_id: 125, text: 'hello', recipientCount: 2, sentAt: '2020-12'},
{_id: 126, text: 'hello', recipientCount: 2, sentAt: '2020-12'},
]
},
{
_id: 10,
sender: 'foo',
messages: [
{_id: 127, text: 'hello', recipientCount: 1, sentAt: '2021-1'},
{_id: 128, text: 'hello', recipientCount: 1, sentAt: '2021-1'},
{_id: 129, text: 'hello', recipientCount: 1, sentAt: '2021-1'},
]
},
{
_id: 11,
sender: 'cap',
messages: [
{_id: 130, text: 'hello', recipientCount: 2, sentAt: '2019-2'},
{_id: 131, text: 'hello', recipientCount: 2, sentAt: '2019-2'},
{_id: 132, text: 'hello', recipientCount: 2, sentAt: '2019-2'},
]
},
]我们需要输出,这将允许我们创建一个表,显示按发送方和sentAt分组的总recipientCount。也就是说-类似于:
2018-1 2018-2 2018-3 2018-4
foo 3 4 31 18
bar 7 24 11 17
cap 19 6 23 75下一个(也是最后一个)是什么?这个聚合管道的阶段?
提前感谢!
发布于 2022-09-06 10:42:18
只需unwind messages数组和sender和sentAt.Like上的组,如下所示:
db.collection.aggregate([
{
"$unwind": "$messages"
},
{
"$group": {
"_id": {
sender: "$sender",
sentAt: "$messages.sentAt"
},
"count": {
"$sum": "$messages.recipientCount"
}
}
},
{
"$project": {
"sender": "$_id.sender",
"sentAt": "$_id.sentAt",
"count": 1,
"_id": 0
}
}
])操场链接。
https://stackoverflow.com/questions/73620472
复制相似问题