-
唯一索引
值唯一,不能重复
db.users.dropIndexes() // 删除全部索引 > db.users.find() { "_id" : ObjectId("601cf85c7cfdb08d65ff4668"), "name" : "n1", "age" : 20, "gender" : "m", "birth" : ISODate("2000-04-10T00:00:00Z"), "pswd" : "password123", "courses" : [ "Golang", "Gorm", "Gin" ] } { "_id" : ObjectId("601cf85c7cfdb08d65ff4669"), "name" : "n2", "age" : 21, "gender" : "f", "birth" : ISODate("1999-04-10T00:00:00Z"), "pswd" : "password123", "courses" : [ "Golang", "Gorm", "Gin" ] } { "_id" : ObjectId("601cf85c7cfdb08d65ff466a"), "name" : "n3", "age" : 22, "gender" : "m", "birth" : ISODate("1998-04-10T00:00:00Z"), "pswd" : "password123", "courses" : [ "Golang", "Gorm", "Gin" ] } { "_id" : ObjectId("601cf85c7cfdb08d65ff466b"), "name" : "n4", "age" : 23, "gender" : "f", "birth" : ISODate("1997-04-10T00:00:00Z"), "pswd" : "password123", "courses" : [ "Golang", "Gorm", "Gin" ] } { "_id" : ObjectId("601cf85c7cfdb08d65ff466c"), "name" : "n5", "age" : 24, "gender" : "m", "birth" : ISODate("1996-04-10T00:00:00Z"), "pswd" : "password123", "courses" : [ "Golang", "Gorm", "Gin" ] } > db.users.ensureIndex({"pswd": 1}, {"unique": true}) // 在pswd字段上建立索引会报错,因为值重复 { "ok" : 0, "errmsg" : "E11000 duplicate key error collection: test.users index: pswd_1 dup key: { pswd: \"password123\" }", "code" : 11000, "codeName" : "DuplicateKey", "keyPattern" : { "pswd" : 1 }, "keyValue" : { "pswd" : "password123" } }
更改重复数据
> db.users.ensureIndex({"name": 1}, {"unique": true}) > db.users.update({"name": "n1"}, {"$set": {"name": "n2"}}) WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.users index: name_1 dup key: { name: \"n2\" }" } })
插入重复数据报错
> db.users.insert({"name": "n1", "age" : 20, "gender" : "m", "birth" : ISODate("2000-04-10T00:00:00Z"), "pswd" : "password123", "courses" : [ "Golang", "Gorm", "Gin" ]}) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.users index: name_1 dup key: { name: \"n1\" }" } })
-
部分索引
只针对符合某个特定条件的文档建立索引。MongoDB部分索引只为那些在一个集合中,满足指定的筛选条件的文档创建索引。由于部分索引时一个集合文档的一个自己,因此部分索引具有较低的存储需求,并降低了索引创建和维护的性能成本。部分索引通过指定过滤条件来创建,可以在为MongoDB支持的所有索引类型使用部分索引
简单的说:部分索引就是带有过滤条件的索引,即索引只存在于某些文档之上
- 创建部分索引
- 部分索引只为集合中那些满足指定指定的筛选条件的文档创建索引。如果指定partialFilterExpression和唯一约束,那么唯一性约束只适用于满足条件的文档。具有唯一约束的部分的索引不会阻止不符合唯一约束且不符合过滤条件的文档插入
> db.users.ensureIndex({"name": 1}, {"partialFilterExpression": {"age": {"$lte": 22}}}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.users.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.users" }, { "v" : 2, "key" : { "name" : 1 }, "name" : "name_1", "ns" : "test.users", "partialFilterExpression" : { "age" : { "$lte" : 22 } } } ]
- 使用部分索引,只有查询条件中包含部分索引时才会使用
> db.users.find({"name": "n4", age: 20}).explain() { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.users", "indexFilterSet" : false, "parsedQuery" : { "$and" : [ { "age" : { "$eq" : 20 } }, { "name" : { "$eq" : "n4" } } ] }, "queryHash" : "2A9321A4", "planCacheKey" : "314F3EF5", "winningPlan" : { "stage" : "FETCH", "filter" : { "age" : { "$eq" : 20 } }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "name" : 1 }, "indexName" : "name_1", "isMultiKey" : false, "multiKeyPaths" : { "name" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : true, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "name" : [ "[\"n4\", \"n4\"]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "bogon", "port" : 27017, "version" : "4.2.12", "gitVersion" : "5593fd8e33b60c75802edab304e23998fa0ce8a5" }, "ok" : 1 }
索引覆盖查询特性
覆盖查询:
- 所有的查询字段是所有的一部分
- 所有的查询返回字段都在索引中