MongoDB is currently ranked the highest non-relational database (currently ranked fourth), as the first document-type database, involving some of the original concepts and terminology, in order to understand MongoDB, need to understand these concepts and terminology.
ObjectId
As a document database, each document of the MongoDB database must have a "_id" field, which has a unique value and is automatically generated, which is the primary key. The only value is the Objectid.
Objectid is a 12-byte Bson type that is composed of the following:
Composition of the Objectid |
2 |
4 |
7 |
8 |
9 |
|
11 |
Time stamp |
Machine identifiers |
Process ID |
Counter |
54f92930 |
B82f19 |
9305 |
541bcf |
54f92930b82f199305541bcf |
- Timestamp: The first 4 bytes, time stamp (seconds) when generating objectid;
- Machine identifier : three bytes, MongoDB host identifier, generally the host name of the hash value, so that different hosts have different identifiers, the same host on the different MongoDB instances generated by the Objectid machine identifier is the same, However, in a multi-host distributed environment, the machine identifier ensures that the Objectid does not conflict.
- process ID: Again two bytes, PID, machine identifiers ensure that the objectid generated on different hosts in a distributed environment does not conflict, while the process ID ensures that the objectid generated by multiple MongoDB processes does not conflict on the same host.
- counter : Again three bytes, the preceding nine bytes ensure that different mongodb processes on different machines do not conflict with the objectid generated in a timestamp (1 seconds), and the last three bytes to ensure that the objectid generated within a timestamp (within 1 seconds) does not conflict.
Objectid's composition perfectly solves the problem of primary key uniqueness under high concurrency in distributed environments. In a similar application scenario, you can learn from it.
Objectid Example:
> MONGO
MongoDB Shell version:3.0.0
Connecting To:test
Welcome to the MongoDB shell.
For interactive help, type ' help '.
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the Support group
Http://groups.google.com/group/mongodb-user
> Use Tonysharpmongodb
Switched to DB Tonysharpmongodb
> DB. Tonysharpcollection.insert ({Name: "Tony", Key: "MongoDB3.0"})
Writeresult ({"ninserted": 1})
> DB. Tonysharpcollection.find ()
{"_id": ObjectId ("54F92930B82F199305541BCF"), "Name": "Tony", "Key": "MongoD
B3.0 "}
MongoDB terminology (Continuous update)