当前位置:网站首页>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
边栏推荐
- 软件测试月薪10K如何涨到30K,只有自动化测试能做到
- LeetCode_ Concurrent search set_ Medium_ 399. division evaluation
- Mysql database design concept (multi table query & transaction operation)
- VR全景作品中各式各样的嵌入功能是如何做到的?
- MySQL数据库设计概念(多表查询&事务操作)
- [advanced C language] data storage [part I] [ten thousand words summary]
- MySql的MyISAM引擎切换InnoDB时报错Row size too large (&gt; 8126)解决
- SPSS introductory notes
- 中国 璞富腾酒店及度假村旗下酒店推出全新水疗产品共庆6月11日全球健康日
- 我的第一部作品:TensorFlow2.x
猜你喜欢

VR全景作品中各式各样的嵌入功能是如何做到的?

【C语言进阶】数据的存储【上篇】【万字总结】
![[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure](/img/94/c9c7935aa0c98eb39a34377ad02b10.png)
[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure

软件测试月薪10K如何涨到30K,只有自动化测试能做到

Morris traversal of binary tree

禁止摆烂,软件测试工程师从初级到高级进阶指南,助你一路晋升
![MySQL advanced Chapter 1 (installing MySQL under Linux) [i]](/img/f9/60998504e20561886b5f62eb642488.png)
MySQL advanced Chapter 1 (installing MySQL under Linux) [i]

2022.05.27 (lc_647_palindrome substring)

Mysql8.0 (summary of new features)

C (pointer-02)
随机推荐
My first work: tensorflow2 x
Office technical lecture: punctuation - Chinese - Daquan
Prospect of database firewall technology [final chapter]
VR全景如何应用在家装中?体验真实的家装效果
如何在VR全景作品中添加独立热点?
2022.05.28 (lc_5_longest palindrome substring)
【C语言进阶】数据的存储【上篇】【万字总结】
[advanced C language] data storage [part I] [ten thousand words summary]
MySql的MyISAM引擎切换InnoDB时报错Row size too large (&gt; 8126)解决
长颈鹿的脖子变长其实不是因为吃高处的树叶?科学家在1700万年前的化石中找到答案
Performance and high availability analysis of database firewall
恭喜 | 医学院那洁课题组通过多组学分析揭示JUNB在体外分化人造血祖细胞过程中的功能
618大促将至,用AI挖掘差评,零代码实现亿级评论观点情感分析
软件测试月薪10K如何涨到30K,只有自动化测试能做到
Source code analysis of Tencent libco collaborative process open source library (II) -- persimmon starts from the soft pinch, and the sample code officially begins to explore the source code
Computer:成功教你如何使用一招—就能找回以前的密码(曾经保存的密码但当前显示为******号的密码)
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
【01】每一位优质作者都值得被看见,来看看本周优质内容吧!
MySQL advanced Chapter 1 (installing MySQL under Linux) [i]
2022.05.23 (lc_300_longest increment subsequence)