所以我在使用 Robo 3T 的 mongodb 4.2.1 上遇到了这个问题。我想通过将一个字段移动到另一个对象中来更新特定文档。
update()像这样使用效果很好。
db.getCollection('myCollections').update(
{
randomId: ObjectId("......."),
},
[
{ $set: { "myObject.myField": "$myField" } },
{ $unset: [ "myField" ] }
])
Run Code Online (Sandbox Code Playgroud)
但是当我想像这样更新我的所有文档时updateMany()。
db.getCollection('myCollections').updateMany(
{
randomId: ObjectId("......."),
},
[
{ $set: { "myObject.myField": "$myField" } },
{ $unset: [ "myField" ] }
])
Run Code Online (Sandbox Code Playgroud)
我有一个错误
Failed to execute script.
Error: the update operation document must contain atomic operators
Details:
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:625:1
@(shell):1:1
Run Code Online (Sandbox Code Playgroud)
我没有尝试使用 shell,但我想它会告诉我同样的事情。
之前的文档示例
{
_id: ...,
randomId: ObjectId(...),
myField: 0.5
myObject: {
value1: 1,
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
后
{
_id: ...,
randomId: ObjectId(...),
myObject: {
value1: 1,
myField: 0.5,
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3999 次 |
| 最近记录: |