Error: the update operation document must contain atomic operators

Hi Team,
The following query exexcuted on command prompted (On windows CMD)
but getting below error , so where i did mistake

MongoDB Enterprise mongos> db.test.updateMany({}, [{$set:{ createdAt:{$toDate:“$_id”}}},{upsert:false, multi:true }])
2023-05-01T19:26:46.906+0530 E QUERY [js] Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:625:1
@(shell):1:1

MongoDB Enterprise mongos> db.version()
4.0.4
MongoDB Enterprise mongos>

The square brackets before the $set and after the multi:true.

The second argument needs to be an update object/document. With the square brackets, you are passing an array.

Getting error afte the square brackets removed before the $set and after the multi:true

The second argument updated objecct/document “createdAt”: {$toDate: “$_id”} .

Here i would want add new field on exiting document based on “_id”

Can i modify query please let me know ?

MongoDB Enterprise mongos> db.test.updateMany({ }, {$set:{“createdAt”: {$toDate: “$_id” }}},{upsert:false, multi:true })
2023-05-01T20:27:02.293+0530 E QUERY [js] WriteError: The dollar ($) prefixed field ‘$toDate’ in ‘createdAt.$toDate’ is not valid for storage. :
WriteError({
“index” : 0,
“code” : 52,
“errmsg” : “The dollar ($) prefixed field ‘$toDate’ in ‘createdAt.$toDate’ is not valid for storage.”,
“op” : {
“q” : {

            },
            "u" : {
                    "$set" : {
                            "createdAt" : {
                                    "$toDate" : "$_id"
                            }
                    }
            },
            "multi" : true,
            "upsert" : false
    }

})
WriteError@src/mongo/shell/bulk_api.js:461:48

I understand better your use-case.

You want to use update with aggregation because you want your new field be calculated from the existing field _id. That explains why you add square brackets at first.

For this to work, you need brackets (unlike what I mentioned). The real issue was that the closing bracket was at the wrong place. It has to be after the 3 closing curly braces rather than after the upsert: and multi: option object. To be clear, rather than

try

db.test.updateMany({}, [{$set:{ createdAt:{$toDate:"$_id"}}}],{upsert:false, multi:true })

Please read Formatting code and log snippets in posts before posting code or documents.

Same error, i used as you mentoned query…

MongoDB Enterprise mongos> db.test.updateMany({}, [{$set:{ createdAt:{$toDate:“$_id”}}}],{upsert:false, multi:true })
2023-05-02T21:32:42.487+0530 E QUERY [js] Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:625:1
@(shell):1:1

May be your version is to old and it does not support update with aggregation.

Hi @hari_dba

I agree with @steevej

I tried this in MongoDB 4.0 series and see an identical error message:

> db.version()
4.0.13

> db.test.updateMany({}, [{$set:{a:1}}])
2023-05-03T11:01:38.333+1000 E QUERY    [js] Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:625:1

Then I tried the same in MongoDB 4.2 series:

> db.version()
4.2.22

> db.test.updateMany({}, [{$set:{a:1}}])
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Since MongoDB 4.0 series is out of support per April 2022, I encourage you to consider moving to a supported version. This will also provide you with the updates with aggregation capabilities that you need.

Note: MongoDB 4.2 series is also out of support as per April 2023. I’m using it here for illustration purposes only.

Best regards
Kevin

2 Likes