Documents in MongoDB are required to have a key, _id, which uniquely identifies them.
Document IDs: _id
Every MongoDB document has an _id field as its first attribute. This value usually a BSON ObjectId. Such an id must be unique for each member of a collection; this is enforced if the collection has an index on _id, which is the case by default.
If a user tries to insert a document without providing an id, the database will automatically generate an _object id and store it the _id field.
Users are welcome to use their own conventions for creating ids; the _id value may be of any type so long as it is a unique.
The BSON ObjectId Datatype
Although _id values can be of any type, a special BSON datatype is provided for object ids. This type is a 12-byte binary value designed to have a reasonably high probability of being unique when allocated. All of the officially-supported MongoDB drivers use this type by default for _id values. Also, the Mongo database itself uses this type when assigning _id values on inserts where no _id value is present.
In the MongoDB shell, ObjectId() may be used to create ObjectIds. ObjectId(string) creates an object ID from the specified hex string.
> x={ name: "joe" }
{ name : "joe" }
> db.people.save(x)
{ name : "joe" , _id : ObjectId( "47cc67093475061e3d95369d" ) }
> x
{ name : "joe" , _id : ObjectId( "47cc67093475061e3d95369d" ) }
> db.people.findOne( { _id: ObjectId( "47cc67093475061e3d95369d" ) } )
{ _id : ObjectId( "47cc67093475061e3d95369d" ) , name : "joe" }
> db.people.findOne( { _id: "47cc67093475061e3d95369d" } )
{ _id : ObjectId( "47cc67093475061e3d95369d" ) , name : "joe" }
BSON ObjectID Specification
A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Here's the schema:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 |
| time | machine |
pid | inc |
Document Timestamps
One useful consequence of this specification is that it provides documents with a creation timestamp for free. All of the drivers implement methods for extracting these timestamps; see the relevant api docs for details.
Comments (4)
IF YOU HAVE A QUESTION, POST IT TO THE USER GROUP.
These pages are fine for comments, but for questions, your best bet will always be the MongoDB User Group.Dec 31
Anonymous says:
Why not just use UUIDs? They are a standard and because they use MAC address you...Why not just use UUIDs? They are a standard and because they use MAC address you can absolutely rely upon their uniqueness across machines. That might be important if you are eg: replicating data between databases where new items might be inserted at either side.
Jan 12
Kyle Banker says:
Quick answer: because these 12-byte ids can be generated faster and takes up les...Quick answer: because these 12-byte ids can be generated faster and takes up less space, esp. when indexed.
Jan 27
Mathias Stearn says:
Also these should be created in roughly increasing order and transparently encod...Also these should be created in roughly increasing order and transparently encode the time of creation. Most of the driver libs support accessing this time which saves users from adding a "created_at" field and possibly and index on that field.
Jan 30
Anonymous says:
Is there any way to search for the last 2 byte? I really don't want to user obje...Is there any way to search for the last 2 byte? I really don't want to user objectids in my web application's urls.
Add Comment