Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

robo3t

So I have this issue with mongodb 4.2.1 using Robo 3T. I want to update specific documents, by moving a field inside another one which is an object.

Using update() like this works fine.

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

But when I want to update all my documents using updateMany() like this.

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

I have an error

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

I didn't try using the shell but I suppose it will tell me the same thing.

Edit

Example of a document before

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

After

{
  _id: ...,
  randomId: ObjectId(...),
  myObject: {
    value1: 1,
    myField: 0.5,
    ...
  }
  ...
}
like image 318
A.Fe Avatar asked Nov 20 '19 13:11

A.Fe


People also ask

What is atomic operator in MongoDB?

In MongoDB, a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document.

Is updateOne Atomic?

updateOne()A write operation on a single document in MongoDB is atomic. When fields must be updated at the same time, embedding them within the same document ensures that the fields can be updated atomically.

What is $Set in MongoDB?

$set outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields . Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

What is wrong with update operation in MongoDB?

Error during migration, MongoError: the update operation document must contain atomic operators. Doesn’t seem to be an issue with the filtering or projection as it’s grabbing the records and generating the array fine (23539 documents to update).

What is the second parameter of updateone () and updatemany ()?

The second parameter of updateOne () and updateMany () must by an Object, so basically you are using a wrong syntax, try like this instead: Thanks for contributing an answer to Stack Overflow!

What is lexicographic order in MongoDB?

In MongoDB 4.4 and earlier, update operators process all document fields in lexicographic order. In MongoDB 5.0 and later, "a.2" is processed before "a.10" because 2 comes before 10 in numeric order. In MongoDB 4.4 and earlier, "a.10" is processed before "a.2" because 10 comes before 2 in lexicographic order.

What does the third argument of the MongoDB search query mean?

The third argument, optional, contains the options. means that, in case there are no documents satisfying the search criteria stated in the first argument, Mongo should create it.


2 Answers

My bad. I just tried with mongo shell and it works fine. Should stop using robo 3T for update.

Sorry for the bother and thanks for the answers

like image 80
A.Fe Avatar answered Sep 17 '22 02:09

A.Fe


Update the document using $rename update operator; it is just renaming the field.

db.upd.updateOne(
  { randomId: ObjectId("xyz")},
  { $rename: { myField: "myObject.myField" } }
}
like image 39
prasad_ Avatar answered Sep 20 '22 02:09

prasad_