①唯一索引

唯一索引的目的是为了让数据库的某个字段的值唯一,为了确保数据的都是合法的,但是唯一索引在插入数据时会对数据进行检查,一旦重复会抛出异常,效率会比较低,唯一索引只是保证数据库数据唯一的最后一种手段,而不是最佳方式,更不是唯一方式,为了保证效率最好采用别的解决方案来保证数据的唯一合法性,尽量减少数据库的压力。

②唯一索引
> db.foo.ensureIndex({"username": 1}, {"unique": true})
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.foo.insert({"username": "mengday", "email": "mengday@163.com", "age": 26})
WriteResult({ "nInserted" : 1 })
// username 重复会报错
> db.foo.insert({"username": "mengday", "email": "mengday2@163.com"})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "E11000 duplicate key error collection: test.foo index: username_1 dup key: { : \"mengday\" }"
        }
})
 
// 第一次 插入不包含索引键的文档,插入成功,不包含索引键系统会默认为索引键的值为null
> db.foo.insert({"email": "mengday3@163.com"})
WriteResult({ "nInserted" : 1 })
 
// 第二次插入不包含唯一索引的键,插入失败,因为不包含键,键的值就null,第一次已经有一个值为null, 再插入null,就是重复
> db.foo.insert({"email": "mengday4@163.com"})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "E11000 duplicate key error collection: test.foo index: username_1 dup key: { : null }"
        }
})
 
// 对多个字段创建唯一索引(关系数据库中的联合主键)
db.user.ensureIndex({"username": 1, "nickname": 1}, {"unique": true})
>
③稀疏索引
稀疏索引:对不存在的键就不进行索引,也就是该文档上没有建立索引,索引条目中也不包含 索引键为null的索引条目,所以再次插入不包含索引键的文档不会报错,直接插入。注意:稀疏索引不光和唯一索引配合使用,也可以单独使用
 
> db.foo.drop()
true
> db.foo.ensureIndex({"username": 1}, {"unique": true, "sparse": true})
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.foo.insert({"email": "mengday1@163.com"})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({"email": "mengday2@163.com"})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({"username": "mengday3", "email": "mengday3@163.com"})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({"username": "mengday3", "email": "mengday3@163.com"})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "E11000 duplicate key error collection: test.foo index: username_1 dup key: { : \"mengday3\" }"
        }
})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

第27章:MongoDB-索引--唯一索引的更多相关文章

  1. mysql 区间锁 对于没有索引 非唯一索引 唯一索引 各种情况

    The locks are normally next-key locks that also block inserts into the "gap" immediately b ...

  2. 一个Web报表项目的性能分析和优化实践(四):MySQL建立索引,唯一索引和组合索引

    先大致介绍下项目的数据库信息. 数据库A:主要存放的通用的表,如User.Project.Report等. 数据库B.C.D:一个项目对应一个数据库,而且这几个项目的表是完全一样的. 数据库表的特点 ...

  3. MongoDB分片,唯一索引与upsert

    前言 分片,唯一索引和upsert,表面上看似没有直接联系的几个东西,到底存在怎样的瓜葛呢? 分片 为了保持水平扩展的有效性,分片功能必须保证各个片之间没有直接关联,不需要与其他分片交互就可以独立做出 ...

  4. MongoDB(课时22 唯一索引)

    3.6.1 唯一索引 唯一索引的主要目的是用在某一个字段上,使该字段的内容不重复. 范例:创建唯一索引 db.students.ensureIndex({"name" : 1}, ...

  5. MongoDB 学习五:索引

    这章我们介绍MongoDB的索引,用来优化查询. 索引介绍 数据库索引有些类似书的目录. 一个查询如果没有使用索引被称为表扫描,意思是它必须像阅读整本书那样去获取一个查询结果.一般来说,我们应尽量避免 ...

  6. MongoDB学习笔记——索引管理

    索引 索引能够提升查询的效率.没有索引,MongoDB必须扫描集合中的所有文档,才能找到匹配查询语句的文档. 索引是一种特殊的数据结构,将一小块数据集保存为容易遍历的形式.索引能够存储某种特殊字段或字 ...

  7. TokuMX唯一索引不支持dropDups选项

    TokuMX v1.5.0的唯一索引(unique index)不支持dropDups选项, 如果源数据包含相同目标key的文档,将无法建立唯一索引. 问题场景: 从MongoDB到TokuMX的数据 ...

  8. SQL Server索引 - 聚集索引、非聚集索引、非聚集唯一索引 <第八篇>

    聚集索引.非聚集索引.非聚集唯一索引 我们都知道建立适当的索引能够提高查询速度,优化查询.先说明一下,无论是聚集索引还是非聚集索引都是B树结构. 聚集索引默认与主键相匹配,在设置主键时,SQL Ser ...

  9. 唯一索引 && 主键索引

    唯一索引唯一索引不允许两行具有相同的索引值. 如果现有数据中存在重复的键值,则大多数数据库都不允许将新创建的唯一索引与表一起保存. 当新数据将使表中的键值重复时,数据库也拒绝接受此数据.例如,如果在 ...

随机推荐

  1. Pyqt5的事例讲解

    1.第一个gui程序 import sys from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QMainWind ...

  2. swift 快速创建一些基本控件

    1.tableview private lazy var cellId = "cellId" fileprivate lazy var tv : UITableView = { l ...

  3. ABAP空格和零的问题

    空格 concatenate wa_detail-zz_mark1 '' '' '' wa_detail-kdmat1 into wa_detail-zz_mark1 separated by spa ...

  4. [leetcode]658. Find K Closest Elements绝对距离最近的K个元素

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  5. day 12 内置函数,装饰器,递归函数

    内置函数 内置函数:python给咱们提供了一些他认为你会经常用到的函数,68种      内置函数     abs() dict() help() min() setattr() all()  di ...

  6. Ajax cookie session form组件

    . Cookie是什么 保存在浏览器端的键值对 为什么要有Cookie? 因为HTTP请求是无状态的 Cookie的原理? 服务端可以在返回响应的时候 做手脚 在浏览器上写入键值对(Cookie) 浏 ...

  7. Java泛型:List<?>与List的区别

    为什么说List<?>是type-safe而List不是type-safe的? 1.List<?> compiler看到了你使用了wildcard ?,那么相当于你对compi ...

  8. Redis单机安装

    安装过程 安装环境 ubuntu14.04 server wget http://download.redis.io/releases/redis-3.2.5.tar.gz tar -xzvf ./r ...

  9. 详解php多人开发环境原理

    作为一名php开发人员,有时候一个项目或一个功能我们不能独自完成,就像当一个仓库开发人员大于1,20人的时候,每个人可能开发不同的模块和功能,用代码版本控制工具比如 git 开不同的分支,流程大概是先 ...

  10. BZOJ 3329 - Xorequ - 数位DP, 矩乘

    Solution 发现 $x \ xor \  2x = 3x$ 仅当 $x$ 的二进制中没有相邻的 $1$ 对于第一个问题就可以进行数位DP 了. 但是对于第二个问题, 我们只能通过递推 打表 来算 ...