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: new ObjectId( "47cc67093475061e3d95369d" ) } )
{ _id : ObjectId( "47cc67093475061e3d95369d" ) , name : "joe" }
BSON ObjectID Specification
A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly increasing order. 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.
Sequence Numbers
Traditional databases often use monotonically increasing sequence numbers for primary keys. In MongoDB, the preferred approach is to use Object IDs instead. Object IDs are more synergistic with sharding and distribution.
However, sometimes you may want a sequence number. The Insert if Not Present section of the [Atomic Operations] page shows an example of how to do this.
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