pkumar0
pkumar0

Reputation: 2239

Is ObjectId in mongodb a constant?

Is ObjectId in mongodb a constant or can it change after the creation of the document?

Can update or any other operation change it?

My ObjectId seems to have changed for a couple of documents and I cannot figure out why.

Upvotes: 0

Views: 435

Answers (2)

user3806115
user3806115

Reputation: 56

There is absolutely no way that your object ID would have changed.

The reason being is Since the object ID is created on the following : Year , month, Date , Hours,
Minutes,
Seconds. So, there is Absolutely NO Way in which your _id would have changed.

Moreover refer the following , you can't even Update the object id. Its created by mongoDb for every insertion.

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230531

Nope, _id field (if that's what you mean by ObjectId) is read-only. Other fields of type ObjectId are fully mutable, of course.

My ObjectId seems to have changed for a couple of documents and I cannot figure out why.

Impossible. But, you can create a new document, which is a copy of another document, has all the same fields, but id is different. Then you delete the original document. Et voila, document appears to have id changed.

Check your code, maybe something like that can happen.

Upvotes: 0

Related Questions