The index is used to speed up the query. With an index, the database does not have to perform a full-table scan, it needs to look in the index first, and then find the data based on the index found. MongoDB's index is almost the same as a traditional relational database.
Create an index
Creating an index is the use of the Ensureindex () method in the appropriate collection.
>db.user.ensureindex ({"username": 1})
The index of all the keys to be used to create the query. The document passed to the Ensureindex method is the same as the one passed to sort: 1 or-1 indicates the direction of the index. If the index has only one key, the direction does not matter.
>db.user.ensureindex ({"username": 1, "Age":-1})
In general, if you have an index of n pieces, the query for the first few keys will help, such as the index: {"A": 1, "B": 1, "C": 1,..., "z": 1} actually there is {"a": 1}, {"A": 1, "B": 1} 、... and other indexes.
The disadvantage of creating an index is that there is additional overhead for each insert, update, and delete, because the database needs to perform these operations as well as to mark them in the index of the collection.
issues to consider when building an index
1) What kind of query do you make? Which keys need to be indexed?
2) What is the index direction of each key?
3) How to deal with extensions? Is there an arrangement of different keys to keep the common data in memory more often?
There is no difference between the key index and the normal key index for an inline document .
Index name
Each index in the collection has a string type name that uniquely identifies the index, which the server uses to delete or manipulate the index. By default, the index name resembles the following: Keyname1_dir1_keyname2_dir2 ...
Keynamex represents the key for the index, and DirX represents the direction of the index.
You can specify a custom name by using the ENSUREINDEX option:
>db.user.ensureindex ({"A": 1, "B": 1, "C": 1,..., "z": 1},{"name": "IndexName"})
Unique index
A unique index ensures that the specified key for each document in the collection has a unique value.
>db.user.ensureindex ({"username": 1},{"unique": true})
The default unique in the collection so that _id is created together when the collection is created and cannot be deleted.
Eliminate Duplicates
When an index is created for an existing collection, some values may already be duplicated, and a unique index will fail at this point. The first document that is discovered can be retained by the dropdups option, and the next document with duplicate values is deleted:
>db.user.ensureindex ({"username": 1},{"unique": true, "dropdups": true})
Note : This is prudent, if the data is important, the more secure way is to write a script for preprocessing.
Composite Unique index
Composite unique indexes, the values of a single key can be the same, as long as the values of all keys are combined differently.
>db.user.ensureindex ({"username": 1, "Age": -1},{"unique": true})
using explain and hint
Explain can analyze the query. Instead, hint can force an index to be used:
>db.user.find ({"Age": +, "username": "WANGDH"}). Hint ({"username": 1, "Age": 1})
Manage Indexes
The metadata information for an index is stored in the System.indexes collection of each database, which is read-only. Can only be done by Ensureindex or dropindexes.
1) Modify the index
>db.user.ensureindex ({"username": 1},{"Background": true})
The background option can be executed in the background to avoid blocking requests when the database is indexed.
Creating an index for an existing document is a little faster than creating an index before inserting all the documents.
2) Delete index
You can delete an index by using Dropindexes plus the index name.
>db.runcommand ({"dropindexes": "CollectionName", "index": "IndexName"})
Deleting a collection can also delete the index, and deleting all the documents of the collection does not affect the index.
geo-Spatial index
MongoDB also provides a spatial geo-index and provides related queries
MongoDB Learning Note-04 index