Mongodb 将arrayFilter匹配的字段设置为另一个字段的值

Mongodb 将arrayFilter匹配的字段设置为另一个字段的值,mongodb,Mongodb,注意:这与询问问题无关,因为这些问题不包括数组筛选器匹配 我有以下疑问: db.UserNotifications.updateOne( {u id:ObjectId(“5C5C39E94C9D440D4577E94”), {$set:{ “通知.$[match].Group.ActualUnreadCount:50, 通知。$[match].Group.UnreadCountSnapshot:“通知。$[match].Group.ActualUnreadCount” }}, {阵列过滤器:[{

注意:这与询问问题无关,因为这些问题不包括数组筛选器匹配

我有以下疑问:

db.UserNotifications.updateOne(
{u id:ObjectId(“5C5C39E94C9D440D4577E94”),
{$set:{
“通知.$[match].Group.ActualUnreadCount:50,
通知。$[match].Group.UnreadCountSnapshot:“通知。$[match].Group.ActualUnreadCount”
}},
{阵列过滤器:[{
“match.Group.Id”:“某些Id”
}]});
现在,我要做的是将数组中匹配项的
UnreadCountSnapshot
值设置为相同匹配项的
ActualUnreadCount

相反,
UnreadCountSnapshot
只是设置为字符串
“Notifications.$[match].Group.ActualUnreadCount”

关于如何实现这一点有什么想法吗

以下是集合中的示例文档:

{
“_id”:{
“$oid”:“5C539E94C9D404D4577E94”
},
“通知”:[{
“对象”:{
“_id”:“5e3967cc9fc5720001d7100f”,
“类型”:“交易”
},
“变化”:{
“动词”:“创建”,
“UTC时间”:{
$date:“2020-02-04T12:47:08.796Z”
},
“演员”:[]
},
“集团”:{
“TotalCount”:2,
“实际读取计数”:50,
“未计数快照”:23,
“ReadTimeUtc”:{
$date:“2020-08-26T10:24:32.602Z”
},
“ViewTimeUtc”:{
$date:“2020-08-26T10:24:32.602Z”
}
}
}],
“LastNotificationTimeUtc”:{
$date:“2019-02-21T10:18:57.456Z”
},
“LastDeletedNotificationTimeUtc”:{
$date:“2020-08-26T10:24:30.374Z”
}
}

数组字段不允许将任何参考字段值分配给另一个字段,您可以从MongoDB v4.2使用

db.UserNotifications.updateOne(
  { _id: ObjectId("5c5c39e94c9d4404d4577e94") },
  [{
    $set: {
      Notifications: {
        $map: {
          input: "$Notifications",
          in: {
            $mergeObjects: [
              "$$this",
              {
                $cond: [{
                    $eq: [
                      "$$this.Object._id",
                      "5e3967cc9fc5720001d7100f" // some id
                    ]
                  },
                  {
                    Group: {
                      $mergeObjects: [
                        "$$this.Group",
                        {
                          ActualUnreadCount: 50, // add update number
                          UnreadCountSnapshot: "$$this.Group.ActualUnreadCount"
                        }
                      ]
                    }
                  },
                  {}
                ]
              }
            ]
          }
        }
      }
    }
  }]
)

您不能使用arrayFields将任何现有字段引用分配给任何字段,您需要使用arrayFields将其作为答案,我将为此奖励您分数。如果您添加一些示例文档,这将非常有用,我不清楚哪个字段是arrayFields,哪个字段是object。我用示例文档更新了问题。这是否仍然适用于此?是的,您可以在回答和输出中检查更新的游乐场链接。