Does Mongodb change the ObjectID or _id attribute for a document?

How can I change the identifier of objects? This is not like a regular attribute, so I'm not sure if I can update this value?

I use Ruby with Mongoid, but I cannot figure out how to use only the ruby ​​driver, any help would be appreciated!

+6
source share
2 answers

MongoDB will not allow you to update the _id field of the document, but you can delete the document and insert it into the new _id value.

+13
source

So, when you create the elements; You can specify your own identifier; however, you cannot change it after it is created, since it is a unique identifier for the string.

A bit more in Object ID

If you want to specify them when creating your elements, you can do: {_id: 'YourUniqueID'}, which will override it. Additional information in this link.

Hope this helps!

+9
source

Source: https://habr.com/ru/post/897353/


All Articles