In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
MongoDB unique index
Actual combat
Part1: write at the front
There is a note here in MongoDB's unique index index, which is mainly reflected in the handling of null values, which is repeated in this article.
Overall environment:
MongoDB 3.2.5
Part2: collection content
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"), "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")}
As you can see here, I generated data from 0 to 10 in the helei collection, while I deliberately omitted the key value of username in the i 10 document.
Part3: collection index situation
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}]
Here you can see that unique index: uk_username has been added to the name column
Part4: verification
When another document that does not contain a username key is inserted, an error is thrown
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: warning
If a document does not have a corresponding key, the index stores it as a null.
-- Summary.
If you create a unique index on a key, but insert multiple documents that are missing the index key, the insert fails because one value of the index key value is null already exists in the collection. As the author's level is limited and the writing time is very short, it is inevitable that there will be some errors or inaccuracies in the article. I urge readers to criticize and correct them.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.