线程很旧,但我来这里寻找答案,因此提供了新的解决方案。
在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
)
希望这能帮助像我这样的人。