Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB unique index

Shulou Source: shulou.com Published: 2022-06-01 11:51:04 09月23日 Update

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.

Tags: Index document error improper item content place multiple actual combat situation data whole time limited level attention environment author reader middle school student Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Huawei MariaDB MySQL