- Mastering MongoDB 3.x
- Alex Giamas
- 164字
- 2021-08-20 10:10:50
ObjectId
ObjectId is a special data type for MongoDB. Every document has an _id field from cradle to grave. It is the primary key for each document in a collection and has to be unique. If we omit this field in a create statement, it will be assigned automatically with an ObjectId.
Messing with the ObjectId is not advisable but we can use it (with caution!) for our purposes.
ObjectId is:
- 12-bytes
- Ordered; sorting by _id will sort by creation time for each document
- Storing the creation time that can be accessed by .getTimeStamp() in the shell
The structure of an ObjectId:
- a 4-byte value representing the seconds since the Unix epoch
- a 3-byte machine identifier
- a 2-byte process id
- a 3-byte counter, starting with a random value

By their structure, ObjectIds will be unique for all purposes; however since these are generated on the client side, one should check the underlying library's source code to verify that implementation is according to specification.
推薦閱讀
- 大學計算機信息技術(shù)導論
- 構(gòu)建高質(zhì)量的C#代碼
- 計算機圖形學
- 高性能混合信號ARM:ADuC7xxx原理與應用開發(fā)
- Learning Apache Spark 2
- PyTorch深度學習實戰(zhàn)
- Visual Basic從初學到精通
- 智能工業(yè)報警系統(tǒng)
- OpenStack Cloud Computing Cookbook(Second Edition)
- 21天學通Visual C++
- Splunk Operational Intelligence Cookbook
- Visual Basic.NET程序設計
- ESP8266 Robotics Projects
- 51單片機應用程序開發(fā)與實踐
- Ubuntu 9 Linux應用基礎