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” : {
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