I am facing a challenge with updating a field in multiple collection documents. The specific field in question is a DBRef, and my goal is to only change the value of the $ref field.
An example of one of the documents is as follows:
{ "_id" : { "$oid" : "50ab682bd3155502a75c7cf6"} , "codeId" : { "$ref" : "version" , "$id" : { "$oid" : "511cb7d5696bdbaf4c85ebb1"}}}
The desired final outcome should look like this:
{ "_id" : { "$oid" : "50ab682bd3155502a75c7cf6"} , "codeId" : { "$ref" : "code" , "$id" : { "$oid" : "511cb7d5696bdbaf4c85ebb1"}}}
I attempted to achieve this using the following approach:
db.collection.update(
{},
{$set:{"codeId":{$ref:"code"}}},
false,
true
);
However, I encountered an issue where the $id was lost and set as null:
{ "_id" : { "$oid" : "50ab682bd3155502a75c7cf6"} , "codeId" : { "$ref" : "code" , "$id" : { "$oid" : null}}}
My main question now is how can I maintain the $id in this update process?
Appreciate any assistance on this matter.