MongoDBuniqueindex-创新互联
MongoDB unique index
实战
Part1:写在最前
MongoDB的 unique index索引这里有个注意事项,主要体现在对NULL值的处理上,本文加以复现。
整体环境:
MongoDB 3.2.5
Part2:集合内容
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这些数据,而i:10文档中我故意漏写username这一个键值
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:验证
当再有一个不包含username键值的文档被插入时,会抛出错误
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:警告
如果一个文档没有对应的键,索引会将其作为null存储。
——总结——
如果对某个键建立了唯一索引,但插入了多个缺少该索引键的文档,由于集合已经存在一个该索引键值的值为null而导致插入失败。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:MongoDBuniqueindex-创新互联
网页网址:http://scyanting.com/article/ccdphg.html