mongodb中的objectid是collection中唯一,还是全db唯一,还是全宇宙唯一? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iiduce
V2EX    MongoDB

mongodb中的objectid是collection中唯一,还是全db唯一,还是全宇宙唯一?

  •  
  •   iiduce 2011-05-27 17:31:58 +08:00 9733 次点击
    这是一个创建于 5316 天前的主题,其中的信息可能已经有所发展或是发生改变。
    mongodb中的ObjectId的12字节是如此构成的:
    0-3这4个字节是时间戳(timestamp)、4-6这3个字节是机器码(machine)、7-8两个字节是进程id(pid)、9-11是程序自增id(increment)

    这样来看,一个object_id是在什么范围内保证了唯一性? collection、db还是全宇宙?
    16 条回复    1970-01-01 08:00:00 +08:00
    real_newbie
        1
    real_newbie  
       2011-05-27 17:58:08 +08:00
    UUID吗? 全宇宙.
    fzcs
        2
    fzcs  
       2011-05-27 18:25:50 +08:00
    UUID 能保证 在人类所能认知的范围里唯一.
    iiduce
        3
    iiduce  
    OP
       2011-05-27 18:33:01 +08:00
    @fzcs @real_newbie
    不是uuid,是mongodb自己构造的一种数据类型,用来做document的默认主键。
    tysx
        4
    tysx  
       2011-05-27 19:18:59 +08:00
    全宇宙唯一
    mrkschan
        5
    mrkschan  
       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:

    0 1 2 3 4 5 6 7 8 9 10 11
    time machine pid inc
    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, r 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.
    fengluo
        6
    fengluo  
       2011-05-27 19:34:44 +08:00
    至少这个collection内啦~
    前几天我一直在为24个字符这么长的id纠结
    iiduce
        7
    iiduce  
    OP
       2011-05-27 20:07:15 +08:00
    @fengluo @mrkschan

    其实我很想确认 objectid在一个db中是不是唯一的。
    相关一些知识我不太完备,所以不能确切推断出来,又没有找到相关的文档说明这个问题的。
    mrkschan
        8
    mrkschan  
       2011-05-27 20:18:41 +08:00
    @iiduce Within the *same* mongodb process... I believe the BSON ObjectId is unique across all collections.

    Note: On a single machine, you can have a master mongodb process and a slave mongodb process. Thus, I have to focus the phrase - *same* mongodb process. Anyway, with this master-slave topology, write operations only occur in master mongodb process and, as a result, the ObjectIds are unique.
    ratazzi
        9
    ratazzi  
       2011-05-27 20:25:27 +08:00
    @mrkschan 我想知道 md5(hostname) 是哪里来的,文档中没有吧,还是在代码里看到的
    mrkschan
        10
    mrkschan  
       2011-05-27 20:31:32 +08:00
    @ratazzi http://is.gd/Gd3yDY mongodb official documentation (english version) ... 不知道要不要跳看 (我在外的世界)
    ratazzi
        11
    ratazzi  
       2011-05-27 20:36:58 +08:00
    @mrkschan 谢谢,终于找到了
    iiduce
        12
    iiduce  
    OP
       2011-05-27 20:38:41 +08:00
    @mrkschan
    清楚一些了,谢谢你的回答。
    mrkschan
        13
    mrkschan  
       2011-05-27 21:02:24 +08:00
    @ratazzi @iiduce 只是 copy 了一下 document 而已.
    ratazzi
        14
    ratazzi  
       2011-05-27 21:17:45 +08:00
    @mrkschan 很惭愧的说,用了一年的 mongodb 那个文档也打开过好几次,就是没看到那里,看来对英文敏感度有待提高啊
    mrkschan
        15
    mrkschan  
       2011-05-27 21:30:47 +08:00
    @ratazzi 我只是先前搜, 如何利用 BSON ObjectId 的 timestamp, 避免在 document 加一 create_timestamp 的 property.
    est
        16
    est  
       2011-05-27 22:34:57 +08:00
    全集群唯一。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     923 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 22:27 PVG 06:27 LAX 14:27 JFK 17:27
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86