MongoDB 4.2.1 - updateMany: Error: the update operation document must contain atomic operators

MongoDB 4.2.1 - updateMany: Error: the update operation document must contain atomic operators

所以我在 mongodb 4.2.1 使用 Robo 3T 时遇到了这个问题。 我想更新特定文档,方法是将一个字段移到另一个对象中。

像这样使用 update() 效果很好。

db.getCollection('myCollections').update(
    {
        randomId: ObjectId("......."),
    },
    [
        { $set: { "myObject.myField": "$myField" } },
        { $unset: [ "myField" ] }
    ])

但是当我想像这样使用 updateMany() 更新我的所有文档时。

db.getCollection('myCollections').updateMany(
    {
        randomId: ObjectId("......."),
    },
    [
        { $set: { "myObject.myField": "$myField" } },
        { $unset: [ "myField" ] }
    ])

我有一个错误

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

我没有尝试使用 shell,但我想它会告诉我同样的事情。

编辑

之前的文档示例

{
  _id: ...,
  randomId: ObjectId(...),
  myField: 0.5
  myObject: {
    value1: 1,
    ...
  }
  ...
}

之后

{
  _id: ...,
  randomId: ObjectId(...),
  myObject: {
    value1: 1,
    myField: 0.5,
    ...
  }
  ...
}

updateOne()updateMany() 的第二个参数必须是 Object,所以基本上你使用了错误的语法,试试这样:

db.getCollection('myCollections').updateMany({
    randomId: ObjectId("......."),
}, {
    $set: {
        "myObject.myField": "$myField"
    },
    $unset: {
        "myField": 1
    }
})

我的错。我刚刚尝试使用 mongo shell 并且效果很好。应该停止使用 robo 3T 进行更新。

抱歉打扰了,感谢您的回答

使用$rename更新文档更新运算符;它只是重命名该字段。

db.upd.updateOne(
  { randomId: ObjectId("xyz")},
  { $rename: { myField: "myObject.myField" } }
}

这是为了帮助像我一样犯了同样错误离开 $set 的人。

所以它给了我问题中指定的相同错误消息。

在查询中也追加 $set

之前:

db.myData.updateOne({key:"value"},{key:"new-value"})

之后:

db.myData.updateOne({key:"value"},{$set:{key:"new-value"}}) 

然后我就可以更新我的数据了。