官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 称多县| 石家庄市| 德兴市| 万宁市| 德令哈市| 阿勒泰市| 织金县| 九龙坡区| 新郑市| 云霄县| 抚顺市| 河间市| 治县。| 句容市| 玉林市| 宜良县| 承德市| 望城县| 西乡县| 周宁县| 长葛市| 什邡市| 昭觉县| 醴陵市| 萍乡市| 博湖县| 师宗县| 安仁县| 连平县| 尉氏县| 阜平县| 青阳县| 台江县| 四会市| 昭苏县| 高唐县| 军事| 赤峰市| 嵩明县| 遵义县| 汤原县|