Part1:寫在最前
MongoDB的 unique index索引這里有個(gè)注意事項(xiàng),主要體現(xiàn)在對(duì)NULL值的處理上,本文加以復(fù)現(xiàn)。
整體環(huán)境:
MongoDB 3.2.5
Part2:集合內(nèi)容
PRIMARY> db.helei.find() { "_id" : ObjectId("58b7ea9544e98b24a5bdcef5"), "i" : 0, "username" : "user0", "age" : 8, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcef6"), "i" : 1, "username" : "user1", "age" : 9, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcef7"), "i" : 2, "username" : "user2", "age" : 82, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcef8"), "i" : 3, "username" : "user3", "age" : 48, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcef9"), "i" : 4, "username" : "user4", "age" : 27, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcefa"), "i" : 5, "username" : "user5", "age" : 53, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcefb"), "i" : 6, "username" : "user6", "age" : 42, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcefc"), "i" : 7, "username" : "user7", "age" : 56, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcefd"), "i" : 8, "username" : "user8", "age" : 5, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b7ea9544e98b24a5bdcefe"), "i" : 9, "username" : "user9", "age" : 56, "created" : "Thu Mar 02 2017 17:49:09 GMT+0800 (CST)" } { "_id" : ObjectId("58b8da80d8509e8f46fd9042"), "i" : "10", "age" : 50, "create" : ISODate("2017-03-03T02:52:48.834Z") }這里可以看到,我在helei集合中生成了i從0到10這些數(shù)據(jù),而i:10文檔中我故意漏寫username這一個(gè)鍵值
Part3:集合索引情況
PRIMARY> db.helei.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "helei.helei" }, { "v" : 1, "key" : { "age" : 1 }, "name" : "idx_age", "ns" : "helei.helei" }, { "v" : 1, "unique" : true, "key" : { "username" : 1 }, "name" : "uk_username", "ns" : "helei.helei", "background" : true } ]
這里可以看出在name列添加了unique index: uk_username
Part4:驗(yàn)證
當(dāng)再有一個(gè)不包含username鍵值的文檔被插入時(shí),會(huì)拋出錯(cuò)誤
PRIMARY> db.helei.insert({i:"11",age:51,create:new Date()})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: helei.helei index: uk_username dup key: { : null }"
}
})
Warning:警告
如果一個(gè)文檔沒有對(duì)應(yīng)的鍵,索引會(huì)將其作為null存儲(chǔ)。
——總結(jié)——
如果對(duì)某個(gè)鍵建立了唯一索引,但插入了多個(gè)缺少該索引鍵的文檔,由于集合已經(jīng)存在一個(gè)該索引鍵值的值為null而導(dǎo)致插入失敗。由于筆者的水平有限,編寫時(shí)間也很倉(cāng)促,文中難免會(huì)出現(xiàn)一些錯(cuò)誤或者不準(zhǔn)確的地方,不妥之處懇請(qǐng)讀者批評(píng)指正。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
名稱欄目:MongoDBuniqueindex-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://aaarwkj.com/article46/ccdphg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、網(wǎng)站排名、Google、微信公眾號(hào)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容