MongoDB Index Command Summary
1. Create an index on a key of the document in the collection:
1. We know that if the index is used properly, the query speed will be greatly improved. If the index is used improperly, the performance of the entire operation may be reduced. Therefore, when creating an index, consider the following points:
(1) What kind of queries will be made and which keys need to be indexed
(2) What is the index direction of each key?
(3) how to cope with expansion, how to sort the key direction, and how to save more common data in the memory
2. Note that 1 and-1 can be used to create indexes in different directions.
3. Use ensureIndex () to create an index on the specified key
4. Create a common index: Use the ensureIndex () function.
Example: db. mytest. ensureIndex ({age: 1 })
// Create an index with the "1" direction for the age key of the document in the mytest set
5. index creation for embedded documents:
Example: db. mytest. ensureIndex ({comment. data: 1 })
// Create an index on the data key of the comment key of the mytest set. The embedded index is the same as the general index!
6. index the sorting (Sort). If there is no index or the key value without index is called for sorting, mongodb will load all the data to the inner for sorting, in this case, if the number is large and the sorting cannot be performed in the memory, an error is returned,
7. The index name is displayed in the format of keyname1_dirx_keyname2_dirx by default. keyName1 indicates the default index name, And dirx indicates the direction of the created index.
8. View index names: indexs. find ()
Example: db. mytest. indexs. find ();
// View all indexes in the mytest set
9. Custom index name when creating an index:
Example: db. mytest. ensureIndex ({age: 1}, {name: 'indexname '})
// In the mytest set, the document's age key is an index of 1 and the index name is indexname.
10. Unique index: the unique index ensures that the specified key of each document in the SET has a unique value.
Example: db. mytest. ensureIndex ({name: 1}, {unique: true })
// Create a unique index for the name key of the document in the mytest set. The name key value of the document in the set will not be repeated!
11. eliminate duplicate values: When we create a document in a set and some documents have duplicate values, we create a unique index, if you select dropDups, the document to which the first index is saved will be deleted!
Example: db. mytest. ensureIndex ({name: 1}, {unique: true, dropDups: true })
// The name key of the document in the mytest set is a unique index with the direction of '1'. If the name key value of the document is repeated, the first retrieved value is retained, the subsequent documents will be deleted.
12. Compound unique index: This is very similar to the key of a structured database table. If the key of this table is determined by an attribute, the value of this attribute cannot be the same, if the key is composed of two or more attributes, You can uniquely identify a row of data as long as the values of these attributes are combined, and the values of a single attribute can be the same, this is the same as the unique index.
Example: db. mytest. ensureIndex ({name: 1, id: 1}, {unique: true })
// Create a composite unique index with the name id key of the document under the mytest set. The name or id of the document under the mytest set can be repeated, and the two values can only determine one document together, it cannot be repeated!
13. Use the explain () tool to View Details:
Example: db. mytest. find (). explain ()
// The mytest set contains 8 Documents
// Cursor: 'basiccursor 'indicates that no index is used for this query. This is normal because the query has no conditions.
// Nscanned: the number of documents queried by the database.
// N indicates the number of documents returned by the data
// Millis: the query time in milliseconds.
14. Check indexes with query Conditions
Example: db. mytest. find ({age: 2}). explain ()
// The data in the first row changes. The btree index is used and the index name is age_1.
// Nscanned: The document tree to be queried is changed to 1. Because of the index, full table scan is not performed, so the number of documents to be searched is reduced.
2. Manage indexes:
1. Modify indexes: We know that creating an index is time-consuming and resource-consuming. During the index creation process, all database requests are blocked, in order to be able to request the database normally, we can establish the index in the background for execution.
2. You can use the {background: true} Option to establish an index in the background and execute the index. This allows you to request the database normally. However, the index creation is slow!
3. delete an index: collection. dropIndex ({xxx: 1/-1 })
Example: db. mytest. dropIndex ({age: 1 })
// Index created from the document age key in the mytest set
4. Delete All indexes: dropIndexes ()
Example: db. mytest. dropIndexes ()
// Delete all the indexes created on multiple keys of the document under this set, including the _ id indexes. You can see the results through running!
5. As mentioned earlier, mongodb uses 1,-1 to represent the direction of index creation, while mongodb can also create a geospatial index, the establishment of a geospatial index is indicated by '2d 'instead of 1,-1.
6. Create a 2d index: ensureIndex () is required to create a key for a geospatial index: the key value 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 multiple data entries
Create a geospatial index:
Example: db. mytest. ensureIndex ({"map": "2d "})
7. geospatial index query: use $ near
Find the two closest points from [5, 5]: db. mytest. find ({map: {"$ near": [5, 5]}). limit (2)
8. Search by image: Square {"$ within": {"# box": [[2, 2], [6, 6]}
Example: db. mytest. find ({"map": {"$ within": {"$ box": [[], []})
9. Search by image: Circle {"$ within": {"# center": [[3, 3], 1]}
Example: db. mytest. find ({"map": {"$ within": {"$ center": [[3, 3], 1]})
// [[3, 3], 1] Use 3 as the center and 1 as the radius to find the point from the center to the far
Note: Here we only summarize the index-related commands in mongodb, but how to build a high-performance index and index-related knowledge points to learn more about MYSQL indexes, because their indexing philosophy is the same!