Skip to content

Instantly share code, notes, and snippets.

@liwh
Created February 17, 2011 07:14
Show Gist options
  • Select an option

  • Save liwh/831209 to your computer and use it in GitHub Desktop.

Select an option

Save liwh/831209 to your computer and use it in GitHub Desktop.
introduce the feature of mongodb
1._id and Objectids
每一个document在MongoDB中保存时,都必须有一个"_id"键,这个键可以为任何类型,默认的是一个ObjectId.在一个单独的collection中,每一个document的"_id"必须是唯一的,这样,collection里的每一个document都能被唯一识别.这个relational database也是一样的.
2.ObjectIds
ObjectId是_id的默认类型.它被设计为轻量级的,与此同时,它也很容易生成全局唯一的方式,在不同的机器上.这就是MongoDB利用ObjectIds,而不用传统的,如自动增长的主键的原因.传统的方式很难而且耗时去同步自动增加的主键值在多个服务中.因为MongoDB是从分布式的数据中设计出来的.处理多节点是一个重要的考虑的因素.这个ObjectId类型,如我们看到的,很容易在共享的环境下生成.也类似传统数据库中uuid的主键生成方式.
ObejctIds利用12个字节来存储,这里用24位十六进制来表示.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment