理解MongoDB默認的ObjectID-創(chuàng)新互聯(lián)

BSON ObjectID Specification

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. The format:

成都創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目做網(wǎng)站、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元洛隆做網(wǎng)站,已為上家服務,為洛隆各地企業(yè)和個人服務,聯(lián)系電話:18980820575

0 1 2 3 4 5 6 7 8 9 10 11
time machine pid inc

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

BSON ObjectIds can be any 12 byte binary string that is unique; however, the server itself and almost all drivers use the format above.

分段查看ObjectId的指令及結(jié)果如下:

  1. > db.test.findOne()._id.toString()  
  2. "50c6b336ba95d7738d1042e3"

ObjectId占用12字節(jié)的存儲空間,由“時間戳” 、“機器名”、“PID號”和“計數(shù)器”組成。使用機器名的好處是在分布式環(huán)境中能夠避免單點計數(shù)的性能瓶頸。使用PID號的好處是支持同一機器內(nèi)運行多個mongod實例。最終采用時間戳和計數(shù)器的組合來保證唯一性。

時間戳

確保ObjectId唯一性依賴的是時間的順序,不依賴時間的取值,因此集群節(jié)點的時間不必完全同步。既然ObjectId已經(jīng)有了時間戳,那么在文檔中就可以省掉一個時間戳了。在使用ObjectID提取時間時,應注意到MongoDB允許各節(jié)點時間不一致這一細節(jié)。

下面是查看時間戳的兩種寫法:

  1. > db.test1.findOne()._id.getTimestamp()
  2. "2012-12-12T03:52:45Z"

機器名

機器名通過Md5加密后取前三個字節(jié),應該還是有重復概率的,配置生產(chǎn)集群時檢查一下總不會錯。另外,我也注意到重啟MongoDB后MD5加密結(jié)果會發(fā)生變化,在利用ObjectID提取機器名信息時需格外注意。

PID號

注意到每次重啟mongod進程后PID號通常會發(fā)生變化就可以了。

計數(shù)器

計數(shù)器占3個字節(jié),表示的取值范圍就是256*256*256-1=16777215。不妨認為MongDB性能的極限是單臺設(shè)備一秒鐘插入一千萬條記錄。以目前的水平看,單臺設(shè)備一秒鐘插入一萬條就很不錯了,因此ObjectID計數(shù)器的設(shè)計是夠用的。

循環(huán)插入了一些記錄,下面的查詢中b是循環(huán)計數(shù)器,可以看出我機器上的ObjectId計數(shù)器是按順序增加的:

  1. > parseInt(db.test.findOne({b:1000})._id.toString().substring(28,34),16)  

以下代碼源自:http://www.cnblogs.com/xjk15082/archive/2011/09/18/2180792.html

  1. 構(gòu)建objectId  
  2. publicclass
  3. int
  4. int
  5. int
  6. public
  7. int
  8. true
  1. 機器碼和進程碼的生成  
  2. privatestaticint
  3. static
  4. try
  5. int
  6. new
  7. while
  8. "machine piece post: "
  9. int
  10. intnew
  11. try
  12. catch
  13. class
  14. int
  15. new
  16. "process piece: "
  17. "machine : "
  18. catch
  19. thrownew

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網(wǎng)站名稱:理解MongoDB默認的ObjectID-創(chuàng)新互聯(lián)
當前路徑:http://bm7419.com/article10/hdjgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、全網(wǎng)營銷推廣、品牌網(wǎng)站建設(shè)、做網(wǎng)站、軟件開發(fā)微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設(shè)