index
concept
- And establish given key storage location of the document list control relationship. We can easily use the index to quickly find and reduce traversing times and improve efficiency.
operating
- Creating an index
db.collection_name.ensureIndex()
- Function: Create Index
- Parameters: providing an index of category options
- 1 shows the forward index created for the domain
- -1 means inverse index
- eg creating an index according to domain name
db.class.ensureIndex({'name':1})
- eg according to the domain name, age field created a composite index
db.class.ensureIndex({'name':1,age:1})
- The system automatically creates indexes for _id
- View the index of the current collection
db.collection_name.getIndexs()
- Delete Index
db.collection_name.dropIndex()
- Function: Delete Index
- Parameters: Index Name
- _id index can not be deleted
- eg:
db.class.dropIndex({'name':1,age:1})
- Delete all the index of the current collection except the _id
db.collection_name.dropIndexes()
- Display detailed information lookup operation
explain()
- eg
db.class.find({age:22}).explain()
Index Type
Types of | effect | example |
---|---|---|
Array index | If you create an index on an array domain, each value in the array are created index. By the array will improve the efficiency of a single value queries | db.class.ensureIndex({hobby:1}) |
Sub-document index | A domain value of the document, create sub-index to its document, accelerate the speed to find child looking through documents | db.class.ensureIndex({'parent.child':1}) |
The only index | When the index field value hope to create a unique index have different values, you can also limit threshold by this method | db.class.ensureIndex({name:1},{'unique':1}) |
Covering index | When looking only to obtain the contents of index entries, not to connect other document content. Such an index from the table to get the query results can improve query efficiency | Find an index entry to name only a name db.class.find({name:'a'},{_id:0,name:1}) |
Sparse index (gap index) | There are only creating an index for the document specified domain, not the domain document is not indexed | db.class.ensureIndex({age:1},{sparse:true}) |
Text Index | Using a text index can quickly retrieve the text, which is useful in a long string search, multiple keywords can be matched, separated by a space, such as searching for content that contains spaces, spaces need an escape character (\ "keyword \ "), '-' denotes not contain | 1. Create a text index db.class.ensureIndex({msg:'text',description:'text'}) --- 2. Retrieve (included "keyword1" or "key word 2" does not contain "keyword3") db.class.find({$text:{$search:"keyword1 \"key word 2\" -keyword3"}}) 3. Delete the text index () view index name by getIndex, and then deleted by dropIndex () |
Constraint Index
- The impact insert, delete, modify the efficiency of the data. When the data is modified, the index must be updated simultaneously.
- Index also occupy a certain space, so not suitable for relatively small amount of data when creating an index.
Fixed set
- MongoDB can create a set of fixed size, called a fixed set, a fixed set of excellent properties and is suitable for a lot of scenes. Such as: log processing, temporary cache
- Features:
- Inserted into the input fast
- Order query speed
- Early data can be eliminated
- Create a fixed set
db.createCollection('collection_name',{capped:true,size:10000,max:1000})
- size: indicates the size of a fixed set provided in kb
- max: represents a fixed set up to store how many documents