In the case of MongoDB, the equivalent of an RDBMS primary key would be the autogenerated ObjectId. You can reference this value via the alias _id. Unlike RDBMS sequences or auto-increment fields, however, the MongoDB _id field includes both sequential as well as random elements. It consists of 12 bytes broken down as follows:
- A 4-byte value representing the number of seconds since midnight on January 1, 1970 (Unix epoch)
- A 5-byte random value
- A 3-byte counter (seeded by a random value)
The _id field could potentially be used when conducting cross-collection operations (see the discussion on DBRefs earlier in this chapter). There is a method, ObjectId.valueOf(), that returns a hexadecimal string of 24 characters. However, within your application code, or when performing an operation using the aggregation framework, as this field is actually an object (for example, ObjectId), you would need to perform further conversions before being able to use...