Object IDs

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:

0123456 7891011
timemachine pidinc

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.


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

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.

blog comments powered by Disqus