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,
...
}
...
}