Document IDs: _id
Almost every MongoDB document has an _id field as its first attribute (there are a few exceptions for system collections and some capped collections). 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, other than arrays, so long as it is a unique. Arrays are not allowed because they are Multikeys.
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 |
TimeStamp
This is a unix style timestamp. It is a signed int representing the number of seconds before or after January 1st 1970 (UTC).
Machine
This is the first three bytes of the (md5) hash of the machine host name, or of the mac/network address, or the virtual machine id.
Pid
This is 2 bytes of the process id (or thread id) of the process generating the object id.
Getting timestamp from mongodb id
So your timestamp is:
timestamp = _id.toString().substring(0,8)
and
date = new Date( parseInt( timestamp, 16 ) * 1000 )
参考: http://www.mongodb.org/display/DOCS/Object+IDs