mongoDB聚合:计数在对象数组中的发生并转换为对象
我有一个称为通知
的集合,该集合具有2个字段(为简单起见):
targets
是字符串数组userreads
是对象的数组{userId,readat}
示例:
{
targets: ['group1', 'group2'],
userReads: [
{userId: 1, readAt: 'date1'},
{userId: 2, readAt: 'date2'},
{userId: 3, readAt: 'date3'},
]
}
{
targets: ['group1'],
userReads: [
{userId: 1, readAt: 'date4'}
]
}
所需的输出:
{
groupsNotificationsCount: {
group1: 2,
group2: 1
},
usersNotificationsCount: {
1: 2,
2: 1,
3: 1
}
}
首先我尝试了此汇总:
[{
$match: {
targets: {
$in: ['group/1','group/2']
},
}
}, {
$project: {
targets: 1,
userReads: 1
}
}, {
$unwind: {
path: '$targets'
}
}, {
$match: {
targets: {
$in: ['group/1','group/2']
}
}
}, {
$group: {
_id: '$targets',
countForGroup: {
$count: {}
},
userReads: {
$push: '$userReads'
}
}
}, {
$addFields: {
userReads: {
$reduce: {
input: '$userReads',
initialValue: [],
'in': {
$concatArrays: [
'$$value',
'$$this'
]
}
}
}
}
}]
我对文档中的每个组都有正确的计数, :
{
_id: 'group1',
countForGroup: 2,
userReads: [
{userId: 1, readAt: 'date1'},
{userId: 2, readAt: 'date2'},
{userId: 3, readAt: 'date3'},
{userId: 1, readAt: 'date4'},
]
}
但是现在我不知道该如何从那里继续以获取所需的输出
I have a collection called notifications
that has 2 fields (for simplicity):
targets
is an array of stringsuserReads
is an array of object{userId, readAt}
Example :
{
targets: ['group1', 'group2'],
userReads: [
{userId: 1, readAt: 'date1'},
{userId: 2, readAt: 'date2'},
{userId: 3, readAt: 'date3'},
]
}
{
targets: ['group1'],
userReads: [
{userId: 1, readAt: 'date4'}
]
}
DESIRED OUTPUT:
{
groupsNotificationsCount: {
group1: 2,
group2: 1
},
usersNotificationsCount: {
1: 2,
2: 1,
3: 1
}
}
At first I tried this aggregate:
[{
$match: {
targets: {
$in: ['group/1','group/2']
},
}
}, {
$project: {
targets: 1,
userReads: 1
}
}, {
$unwind: {
path: '$targets'
}
}, {
$match: {
targets: {
$in: ['group/1','group/2']
}
}
}, {
$group: {
_id: '$targets',
countForGroup: {
$count: {}
},
userReads: {
$push: '$userReads'
}
}
}, {
$addFields: {
userReads: {
$reduce: {
input: '$userReads',
initialValue: [],
'in': {
$concatArrays: [
'$value',
'$this'
]
}
}
}
}
}]
I have the right counts for each group in my documents like so:
{
_id: 'group1',
countForGroup: 2,
userReads: [
{userId: 1, readAt: 'date1'},
{userId: 2, readAt: 'date2'},
{userId: 3, readAt: 'date3'},
{userId: 1, readAt: 'date4'},
]
}
But now I have no idea how to go on from there to get my desired output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用
$ facet
的完美用例。您可以通过$ Untind
和$组
$ group 分别处理 groups notificationcount usernotificationcount 。之后,通过使用$ arraytoObject
来构造您的预期表格,然后将其构建为 Mongo Playground 供您参考。
This is a perfect use case for using
$facet
. You can processgroupsNotificationsCount
andusersNotificationsCount
separately by$unwind
and$group
the count. Afterwards by wrangling into array of k-v tuples, you can construct your expected form by using$arrayToObject
Here is the Mongo playground for your reference.