当前位置:网站首页>Mongodb unique index
Mongodb unique index
2022-06-10 19:49:00 【51CTO】
A unique index ensures that the index field does not store duplicate values ; That is, to enforce the uniqueness of index fields . By default ,MongoDB When you create a collection, you create it in id Create a unique index on the field .
from Mongodb4.2 Version start , The functional compatibility version is 4.2( Or higher ),MongoDB A new internal format is used for unique indexes , It's different from the early MongoDB Version incompatible , The new format is applicable to existing unique indexes and newly created indexes / Unique index rebuilt .
1、 Updating mongodb There is a prompt that the index is null when the document field
db.version()
4.2.18
1.2、 Look at the collection index :
1.3、 After reading the official documents, it says : Interested parties can click on the text to view the official documents
If a document has no index field value in the unique index , Then the index will store a null value for this document . Due to unique constraints ,MongoDB Only one document with missing index fields is allowed . If there are multiple documents without the value of the index field or missing the index field , Then the index building will fail due to duplicate key errors .
operation : Delete documents without unique index fields , The update is successful .
2、 unique index :
You can create... In a single field , Create unique index command db.collection.createIndex() take unique Set to true
db.collection.createIndex( <key and index type specification>, { unique: true } )
Insert a piece of data
The unique field is repeatedly inserted
Insert data that does not contain unique index fields
You can also create a composite unique index
Another example is , Consider a collection with the following documents
{ _id: 1, a: [ { loc: “A”, qty: 5 }, { qty: 10 } ] }
db.collection.createIndex( { “a.loc”: 1, “a.qty”: 1 }, { unique: true } )
Unique indexes allow the following documents to be inserted into the collection ,
Because the index is for a.loc and a.qty The combination of values enforces uniqueness , So the following data insertion is successful
边栏推荐
- MySQL (17 trigger)
- LeetCode_并查集_中等_399. 除法求值
- 一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
- Go语学习笔记 - 跨域配置、全局异常捕获 | Web框架Gin(四)
- 个人如何投资理财比较安全?
- 618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code
- SAR图像聚焦质量评价插件
- How is it safe for individuals to invest in financial management?
- Does the giraffe's neck grow longer not because it eats leaves from high places? Scientists have found the answer in fossils 17million years ago
- 騰訊Libco協程開源庫 源碼分析(二)---- 柿子先從軟的捏 入手示例代碼 正式開始探究源碼
猜你喜欢

ESP8266 系统环境搭建
![[web] personal homepage web homework](/img/16/90b7b559e43e7cd6d5e32865eb0c00.png)
[web] personal homepage web homework "timetable", "photo album" and "message board"

掌握高性能计算前,我们先了解一下它的历史

MySQL数据库设计概念(多表查询&事务操作)

My first work: tensorflow2 x

大厂测试员年薪30万到月薪8K,吐槽工资太低,反被网友群嘲?
![MySQL advanced Chapter 1 (installing MySQL under Linux) [i]](/img/f9/60998504e20561886b5f62eb642488.png)
MySQL advanced Chapter 1 (installing MySQL under Linux) [i]

2022.05.28 (lc_516_longest palindrome subsequence)

Detailed interpretation of tph-yolov5 | making small targets in target detection tasks invisible

mysql(17-觸發器)
随机推荐
ESP8266 系统环境搭建
Design and reality of JSP project laboratory management system based on SSM doc
[advanced C language] advanced pointer [Part 1]
[advanced C language] data storage [part I] [ten thousand words summary]
[advanced C language] advanced pointer [Part 2]
618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code
Esp8266 system environment setup
叮咚抢菜-派送时段监听及推送工具
騰訊Libco協程開源庫 源碼分析(二)---- 柿子先從軟的捏 入手示例代碼 正式開始探究源碼
专项测试之「 性能测试」总结
VR全景如何应用在家装中?体验真实的家装效果
个人如何投资理财比较安全?
SQL statement to view the basic table structure and constraint fields, primary codes and foreign codes in the table (simple and effective)
Lingo12 software download and lingo language introduction resources
OFFICE技术讲座:标点符号-中文-大全
Chapter 6 relational data theory exercise
Tencent libco collaboration open source library source code analysis full series summary blog
TiDB - 快速入门,集群搭建
APICloud可视化开发新手图文教程
Super simple course design SSM student management system (including simple addition, deletion, modification and query of source code)