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

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.

主站蜘蛛池模板: 綦江县| 元阳县| 固阳县| 平和县| 周至县| 银川市| 南京市| 阜新市| 宜阳县| 晋城| 分宜县| 揭东县| 阳江市| 洛浦县| 贵南县| 东乡县| 建德市| 上饶县| 阿荣旗| 白银市| 玛纳斯县| 徐闻县| 抚顺市| 平顺县| 定日县| 上思县| 通榆县| 犍为县| 铁力市| 囊谦县| 五家渠市| 阳西县| 兴隆县| 石泉县| 遂平县| 外汇| 当涂县| 颍上县| 郁南县| 高雄县| 芷江|