Py学习  »  ersnh  »  全部回复
回复总数  1
5 年前
回复了 ersnh 创建的主题 » Mongodb更新子文档字段的方法比逐个更新更好[重复]

线程很旧,但我来这里寻找答案,因此提供了新的解决方案。

在MongoDB 3.6+版本中,现在可以使用位置运算符更新数组中的所有项。见 official documentation here .

下面的问题将适用于这里提出的问题。我还用Java MongoDB驱动程序进行了验证,并成功运行。

.update(   // or updateMany directly, removing the flag for 'multi'
   {"events.profile":10},
   {$set:{"events.$[].handled":0}},  // notice the empty brackets after '$' opearor
   false,
   true
)

希望这能帮助像我这样的人。