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.
Example of a document before
{
_id: ...,
randomId: ObjectId(...),
myField: 0.5
myObject: {
value1: 1,
...
}
...
}
After
{
_id: ...,
randomId: ObjectId(...),
myObject: {
value1: 1,
myField: 0.5,
...
}
...
}
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.
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.
$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.
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).
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!
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.
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.
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
Update the document using $rename
update operator; it is just renaming the field.
db.upd.updateOne(
{ randomId: ObjectId("xyz")},
{ $rename: { myField: "myObject.myField" } }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With