Objective: To achieve index creation, query, delete, Explan management and other operating environment:> Db.version () 3.4.7 index creation meets the basic needs: 1; Index increases query speed by 2; In MongoDB, indexes can be created in ascending/descending order by automatic column, easy to sort 3 The default is to use Btree to organize index files, after version 2.4, also allow the establishment of hash index management index commonly used syntax is: Db.c1.createIndex (keypattern[,options]) #keypattern表示索引匹配的字段列, For example, name:1 indicates that the column is indexed in ascending order, [, options] to create an index using another field column, which makes up a composite index db.c1.getIndexes () #查看索引的语法, when the index is not manually created, the system will have an indexed column by default: _id _db.c1.find ({name: ' test10 '}). Explain () #表示查询name列集合集值test10是否有创建索引 one, index creation General index creation method has the basic index, the compound index, the unique index three kinds. 1. Basic Index > Db.c1.createIndex ({newname:1}) {"createdcollectionautomatically": false, "Numindexesbefore": 1, "num Indexesafter ": 2," OK ": 1} #由上可以看出创建了一个基本的单列索引 2, composite index creation > Db.c1.createIndex ({newname:1,age:-1}) 3, unique index The primary function of creating a unique index is only allowed for field values the only common syntax is: Db.c1.ensureIndex (Keypattern[,options])-DEPRECATED, use CreateIndex () instead> Db.c1.ensureIndex ({num:1},{unique:true})
Second, the index of View > Db.c1.getIndexes () 1; View Base Index
2. View composite Index
3. View Unique Index
Iii. Explan Index Validation Creating an index is primarily for faster queries, and after the base index is created, queries based on that column will speed up. A composite index is typically indexed by two or more columns, and if it is a single-column conditional query, the first created column of the field is valid (that is, after the composite index is created, the single-column query will have an index effect), but the index query for the field columns after it has no effect. If the query condition used in the composite index is the column when the composite index is created, the change location of the field condition query is okay using the method:> Db.c1.find ({newname: ' Test5 '},{age:5}). Explain ()
#可以看到它的一个复合查询
Iv. deletion of indexes common syntax: Db.c1.dropIndex (Index) > Db.c1.dropIndex ({age:-1}) {"Nindexeswas": 6, "OK": 1}> db.c1.dropIndex ({ Num:1}) {"Nindexeswas": 5, "OK": 1}> db.c1.dropIndex ({name:1,age:-1}) {"Nindexeswas": 4, "OK": 1}
2, the management of the index of MongoDB learning