Object IDs
Documents in MongoDB required a key, _id, which uniquely identifies them.(只有_id建立索引之后,各个doc的_id才会不同,否则有可能相同) The _id FieldIf a user tries to insert a document without providing an _id field, the database will automatically generate an _object id_ and store it the _id field.(当客户端提供不提供_id时,则服务器来产生。) The _id value may be of any type, other than arrays, so long as it is a unique. If your document has a natural primary key that is immutable we recommend you use that in _id instead of the automatically generated ids. Arrays are not allowed _ids because they areMultikeys. The BSON ObjectId Datatypea special BSON datatype is provided for object ids. 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. BSON ObjectID SpecificationA 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. The format:
the server itself and almost all drivers use the format above. |