Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are default _id fields for MongoDB documents always 24 hex characters?

Tags:

mongodb

As part of my application requirements, I have a limit of 30 characters for an ID field. This is out of my control and I am wondering if the MongoDB default _id fields will work for me. It appears as though the default _id field is 24 characters long. That works for me, but I am wondering if this is likely to change in the future. I am well aware that things can always change, but, for the next year or two, can I expect there to be 24 character default _id fields?

like image 618
ottobar Avatar asked Apr 13 '10 20:04

ottobar


People also ask

Are MongoDB IDS always 24 characters?

Properties of the Object IDAn object ID is 24 characters long with two characters taking up to one byte, thus containing a total of 12 bytes.

What is the _id field in MongoDB?

The _id Field In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

How is _id generated in MongoDB?

Architecturally, by default the _id field is an ObjectID, one of MongoDB's BSON types. The ObjectID is the primary key for the stored document and is automatically generated when creating a new document in a collection.

Is _id unique in MongoDB?

According to MongoDB's manual the answer is yes, it's unique by default: MongoDB creates the _id index, which is an ascending unique index on the _id field, for all collections when the collection is created. You cannot remove the index on the _id field.


1 Answers

They aren't actually 24 characters - they are 12 bytes (24 characters in hex representation). And yes, that will be the case for the foreseeable future.

like image 165
mdirolf Avatar answered Oct 26 '22 22:10

mdirolf