mongodb-将一些字段从数组移动到另一个数组
我有这个简化的MongoDB文档,并且想更改某些内容,因为有很多冗余数据。该字段“ ActiveUserslookup”是聚合的结果,它返回了我想放入第一个用户
array中的数据。
第一个ID:
“ _ ID”:“ 80B1565A-FAF4-4E68-9BD6-8344060E8D3A”
匹配
ID
来自 ActiveUserslookup
与用户ID相同的故事。
[{
"_id": "80b1565a-faf4-4e68-9bd6-8344060e8d3a",
"users": [
{
"_id": "eaa946da-2708-443e-ab4c-b6db357050ca",
"lastactive": {
"$date": {
"$numberLong": "1637922656000"
}
}
},
{
"_id": "4972ba13-6f4e-4943-be07-15802e22e0dd",
"lastactive": {
"$date": {
"$numberLong": "1653286066000"
}
}
},
{
"_id": "6c4a62ce-c6c6-430f-a0cd-d348ec77dbb2",
"lastactive": {
"$date": {
"$numberLong": "1558623982000"
}
}
}
],
"activeUsersLookup": [
{
"_id": "80b1565a-faf4-4e68-9bd6-8344060e8d3a",
"users": [
{
"_id": "eaa946da-2708-443e-ab4c-b6db357050ca",
"activities": 2
},
{
"_id": "6c4a62ce-c6c6-430f-a0cd-d348ec77dbb2",
"activities": 1
}
],
"sumOfActivities": 3
}
]
}]
因此,最终文档或多或少应该看起来像这样:
[{
"_id": "80b1565a-faf4-4e68-9bd6-8344060e8d3a",
"users": [
{
"_id": "eaa946da-2708-443e-ab4c-b6db357050ca",
"lastactive": {
"$date": {
"$numberLong": "1637922656000"
}
},
"activities": 2
},
{
"_id": "4972ba13-6f4e-4943-be07-15802e22e0dd",
"lastactive": {
"$date": {
"$numberLong": "1653286066000"
}
},
"activities": 0
},
{
"_id": "6c4a62ce-c6c6-430f-a0cd-d348ec77dbb2",
"lastactive": {
"$date": {
"$numberLong": "1558623982000"
}
},
"activities": 1
},
"sumOfActivities": 3
]
}]
我尝试过:
{
$addFields: {
'licenses.activities': '$activeUsersLookup.users.activities'
}
}
但是这给了我一个空数组,所以我必须做错了什么。
下一个阶段是将所有这些活动概括为
sumofactivitivitivities
,最后一个阶段将不设置 activeuserslookup
。
我必须做什么魔术才能获得所需的结果? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您对
“ sumofactivities”发布的预期结果没有:3
在用户
数组中有效。假设您正在尝试达到以下结果:
查询有点长:
$ set
- setactiveuserslookup
field aid object。1.1。
$ first
- 从 1.2 。获取第一个文档
1.2。
$ filter
-activeuserslookup
通过匹配_id
activeuserslookup
与中的文档
_id (根文档)。$ set
2.1。 - 设置
用户
数组。2.1.1。
$ map
- 迭代用户> randar中的文档并返回新数组。
2.1.2。
$ MergeObjects
- 将当前文档与活动合并
字段。2.1.3。
$ ifnull
- set活动
as 0如果没有结果从 2.1.4 。返回
2.1.4。
$ getfield
- 从结果 2.1.5 。2.1.5。
$ first
- 从结果 2.1.6 。中获取第一个文档
2.1.6。
array) > _id 当前文档。$ filter
- 过滤activeuserslookup.users
文档通过匹配_id
for用户
2.2。 SET
sumofactivities
字段。$ unset
- 删除activeuserslookup
field。示例mongo playground
I don't think the expected result you posted for the
"sumOfActivities": 3
in theusers
array is valid.Assume that you are trying to achieve the result as below:
The query is a bit long:
$set
- SetactiveUsersLookup
field as object.1.1.
$first
- Get the first document from 1.2.1.2.
$filter
- Filter document(s) fromactiveUsersLookup
by matching_id
for the document inactiveUsersLookup
with_id
(root document).$set
2.1. - Set
users
array.2.1.1.
$map
- Iterate the documents inusers
array and return a new array.2.1.2.
$mergeObjects
- Merge current documents with the documents withactivities
field.2.1.3.
$ifNull
- Setactivities
as 0 if no result returned from 2.1.4.2.1.4.
$getField
- Get theactivities
field from the result 2.1.5.2.1.5.
$first
- Get the first document from the result 2.1.6.2.1.6.
$filter
- Filter theactiveUsersLookup.users
documents by matching_id
for the document (users
array) with_id
for the current document.2.2. Set
sumOfActivities
field.$unset
- RemoveactiveUsersLookup
field.Sample Mongo Playground