当前位置:网站首页>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
边栏推荐
- Before we learn about high-performance computing, let's take a look at its history
- 【C语言】这些经典题型大家都掌握了吗?一文学会这些题
- [advanced C language] data storage [Part 2] [ten thousand words summary]
- Some questions often asked during the interview. Come and see how many correct answers you can get
- frp reverse proxy
- 写作技术文章是留给自己未来的财富
- Go语学习笔记 - 跨域配置、全局异常捕获 | Web框架Gin(四)
- 腾讯Libco协程开源库 源码分析 全系列总结博客
- 一文详解EventMesh落地华为云的探索及实践
- Mixin -- mixed
猜你喜欢

DDD landing practice repeat record of theoretical training & Event storm

ESP8266 系统环境搭建

【 Web 】 page d'accueil personnelle 】 Programme d'études 】 albums de photos 】 babillard d'information 】

【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】

Zabbix Server Trapper远程代码执行漏洞(CVE-2017-2824)

HW蓝队中级面试复盘

Apicloud visual development - one click generation of professional source code

APICloud可视化开发新手图文教程

2022.05.29 (lc_6079_price reduction)

领域驱动设计(六) - 架构设计浅谈
随机推荐
禁止摆烂,软件测试工程师从初级到高级进阶指南,助你一路晋升
100003字,带你解密 双11、618电商大促场景下的系统架构体系
MySQL数据库设计概念(多表查询&事务操作)
Ranked first in China's SDN (software) market share for six consecutive years
面试中经常问到的几个问题,快来看看能答对几道吧
MySql的MyISAM引擎切换InnoDB时报错Row size too large (&gt; 8126)解决
Zabbix Server Trapper远程代码执行漏洞(CVE-2017-2824)
mysql(17-觸發器)
领域驱动设计(六) - 架构设计浅谈
SAR image focusing quality evaluation plug-in
Monotonic stack structure
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
618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code
2022.05.24 (lc_674_longest continuous increasing sequence)
云图说|每个成功的业务系统都离不开APIG的保驾护航
【C语言】这些经典题型大家都掌握了吗?一文学会这些题
Source code analysis of Tencent libco collaboration open source library (III) -- Exploring collaboration switching process assembly register saving and efficient collaboration environment
Domain Driven Design (VI) - Architecture Design
改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
Before we learn about high-performance computing, let's take a look at its history