mongodb的id的唯一性_mongodb中的objectid是collection中唯一,还是全db唯一,还是全宇宙唯一?...

5

2011-05-27 19:26:00 +08:00

@iiduce 你已提過了...

ObjectID = 32bit timestamp + md5(hostname) + process-id + sequence-no. (Full definition is quoted at bottom of this post)

在不同電腦內, process-id 跟 sequence-no 跟 timestamp 是有機會一樣的

跟據 wikipedia (http://is.gd/yZBK2B) md5 hash 不是 collision resistance.

即是說, 是有機會出現 md5(hostname_A) == md5(hostname_B).

所以, ObjectId 一定不是全宇宙唯一.

----

BSON ObjectID Specification (http://is.gd/XcorBb)

A 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. Here's the schema:

0123456 7891011

timemachine pidinc

Here is a breakdown of the sections:

TimeStamp

This is a unix style timestamp. It is a signed int representing the number of seconds before or after January 1st 1970 (UTC).

Machine

This is the first three bytes of the (md5) hash of the machine host name, or of the mac/network address, or the virtual machine id.

Pid

This is 2 bytes of the process id (or thread id) of the process generating the object id.

Increment

This is an ever incrementing value, or a random number if a counter can't be used in the language/runtime.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值