First, set up an index on a key in the Chinese document:
1, we know that if the index is used properly, it will greatly improve the query speed, and if the use of improper may cause the overall performance degradation, the index should consider the following points:
(1), what kind of query will be done, which keys need to be indexed
(2), what is the index direction of each key?
(3), how to deal with the extension, how to sort the direction of the key, so that more commonly used data stored in memory
2. Note here that you can use 1,-1 to set up indexes in different directions when indexing
3. Use Ensureindex () to create an index on the specified key
4. Build a normal index: use the Ensureindex () function
Example: Db.mytest.ensureIndex ({age:1})
MyTest set the age key of the Chinese document to create an index with a direction of "1"
5, the embedded document to establish the index:
Example: Db.mytest.ensureIndex ({comment.data:1})
The index is created on the data key of the comment of this mytest set, and the inline index is the same as the normal index!
6, index the sort (sort), if there is no index, or call a key value not indexed to sort, MongoDB will load all the data into the order, so if the number is large, can not be sorted in memory will be an error,
7, the name of the index is displayed by default according to: Keyname1_dirx_keyname2_dirx format, where the KEYNAME1 is the default index name, DirX is the direction of the established index
8. View of index Name: Indexs.find ()
Example: Db.mytest.indexs.find ();
View all the indexes under the MyTest collection
9. When indexing, the custom index name:
Example: Db.mytest.ensureIndex ({age:1},{name: ' IndexName '})
The age key in the MyTest collection is indexed with a direction of 1, and the index name is IndexName
10. Unique index: Unique index ensures that the specified key for each document in the collection has a unique value
Example: Db.mytest.ensureIndex ({name:1},{unique:true})
The name key of the document in the MyTest collection creates a unique index, so the value of the name key for the file in the collection will not be duplicated!
11, eliminate duplicate values: When we created a document in a collection, and some of the values of some of the documents are duplicated, then we create a unique index, select dropdups This option will be the first index to the document saved, then will be deleted!
Example: Db.mytest.ensureIndex ({name:1},{unique:true,dropdups:true})
The name key of the document under the MyTest collection creates a unique index with a direction of ' 1 ', and if the value of the document's name key is duplicated, the first retrieved value is preserved, and the subsequent document is deleted
12. Composite unique index: This is similar to the key of a structured database table, if the key of the table is a property, the value of this property cannot be the same, and if the key is made up of two or more properties, you can uniquely determine a row of data by combining the values of the multiple attributes, and the value of a single property can be the same , this and the unique index, the definition of composite unique index to express the same meaning
Example: Db.mytest.ensureIndex ({name:1,id:1},{unique:true})
The name ID key of the document under MyTest This collection creates a composite unique index, the name or ID of the document under the MyTest collection can be duplicated, and the two values together can only identify one document and cannot be duplicated!
13. Use the Explain () tool to view specific information:
Example: Db.mytest.find (). Explain ()
There are 8 documents in the MyTest collection
Cursor: ' Basiccursor ' indicates that the query does not use an index, which is normal because the query has no conditions
Nscanned: How many documents are found on behalf of the database
n represents the number of documents returned by the data
Millis: The number of milliseconds represents the time the query was used
14, there are query criteria to look at the index
Example: Db.mytest.find ({age:2}). Explain ()
The first row of data has changed, using the Btree index, the index name is Age_1
nscanned: The document tree of the query becomes one because there is an index, so there is no full table scan, so the number of documents found is smaller
Second, management index:
1, modify the index: we know that when establishing the index dues, waste of resources, during the indexing, all the database requests will be blocked, in order to be able to request the database normally we can set up the index in the background to execute.
2, using the {background:true} option to set up the index in the background execution, so that the database can normally request, but one drawback is that the establishment of the index is relatively slow!
3. Delete Index: Collection.dropindex ({xxx:1/-1})
Example: Db.mytest.dropIndex ({age:1})
The index created on this key from the age of the document in this collection from MyTest
4. Delete all indexes: Dropindexes ()
Example: Db.mytest.dropIndexes ()
Delete all indexes created on multiple keys of the document under this collection, including the index on the _id, which can be seen by running the results!
5, before the establishment of the index of MongoDB to use 1,-1 to express direction, and MongoDB can also establish a geospatial index, the establishment of a geospatial index using ' 2d ' to represent and no longer 1,-1
6. Build 2d Index: Ensureindex () the key to establish a geospatial index is required: The value of the key must be a pair of values, or a data or a pair of key-value pairs
Example: Db.mytest.insert ({age:1,name: ' name1 ', map:[2,2]})
Db.mytest.insert ({age:2,name: ' name2 ', map:[2,3]})
....
Db.mytest.insert ({age:5,name: ' Name5 ', map:[5,6]})
Add more than one piece of data
To create a geospatial index:
Example: Db.mytest.ensureIndex ({"Map": "2d"})
7. Geo-Spatial index query: use $near
Find two closest points from [5,5]: Example: Db.mytest.find ({map:{"$near": [5,5]}}). Limit (2)
8. Follow the graph to find: square {"$within": {"#box": [[[2,2],[6,6]]}}
Example: Db.mytest.find ({"map": {"$within": {"$box": [[2,2],[6,6]}}})
9. Search by Graph: circle {"$within": {"#center": [[[3,3],1]}}
Example: Db.mytest.find ({"map": {"$within": {"$center": [[3,3],1]}}})
[[3,3],1] with 3,3 as the center, with 1 as the radius, find the point from the center of the near to far
Note: Here is just a summary of the index-related commands in MongoDB, and how to build high-performance index and index-related knowledge points in-depth learning can learn the index of MySQL, because the concept of their index is the same!
MongoDB Index Related Commands summary