Search code examples
mongodbmongodb-compass

How do I edit or update DBRef fields through mongodb compass


I am new to mongo and am trying to change a DBRef set on a document. The value is currently set to 6645230edd4f91c989b0957a and I wish to update it to 6668e6a7de76888362c8a718

Compass will not allow me to edit this value. I can't even create a new field of DBRef type and, indeed, the field is typed as 'array'. But if I create a new array I cannot match the structure.

How do I create or edit these dbrefs through compass?


Solution

  • In MongoDB, documents are actually stored as BSON and in many cases, can be interacted as JSON. If you use the JSON view in MongoDB compass, you should be able to see the format of the field:

    { "$ref" : <value>, "$id" : <value>, "$db" : <value> }
    

    In this sense, you can just update the $id field in the JSON view to achieve what you are expecting. Similarly, the referenced database and collection can be changed through updating $db and $ref field respectively.