I. Indexing operation
Indexes are born to optimize query speed, MongoDB indexes are almost identical to other relational databases, such as Mysql,oracle, and their index optimization experiences apply to MongoDB as well.
1. CREATE index
Indexing in MongoDB is done through the ensureindex operation. The following tests the performance difference between using an index and not using an index, using the Explain function for query performance analysis.
To insert test data:
Queries that do not use indexes:
Queries using the index:
As seen from the above tests, using the appropriate index lookup can greatly optimize query efficiency.
2. Delete Index
The meta information for the index is stored in the System.indexes collection of each database, which is a reserved collection and cannot be inserted or deleted from the document.
Operations can only be performed through Ensureindex or dropindexes.
Second, unique index
A unique index ensures that the specified key for each document in the collection has a unique value. MongoDB establish a unique index in the following ways:
Db.person.ensureIndex ({"Name": 1},{"unique": true})
If there is no corresponding key, the index will store it as a null. Therefore, if a unique index is established on a key, but multiple documents missing the key are inserted, the insertion fails because the document contains a null value.
Third, combined index
The establishment of a composite index is achieved by passing a document with multiple key values for the first parameter of Ensureindex. Composite indexes are suitable for optimization of multiple-condition queries, and in general, queries for the first few keys will help if the index contains n keys. For example, there is an index {"a": 1, "B": 1, "C": 1,..., "z": 1}, actually there is {"a": 1},{"a": 1, "B": 1},{"a": 1, "B": 1, "C": 1}, etc. index, but use {"B": 1},{"a": 1, "C" : Queries indexed for 1} are not optimized.
Iv. Geo-Spatial index
MongoDB's geo-spatial index is useful for locating n locations closest to the current location and is often used in LBS applications for processing plane coordinates.
1. Create Spatial Index
Db.map.ensureIndex ({"GPs": "2d"})
The value of the "GPs" key must be in some form a pair of values: an array of two elements or an inline document containing two keys (random key names), as follows:
{"GPs": [0,100]}
{"GPs": {"X": 0, "Y":}}
{"GPs": {"latitude": 0, "Longitude": 100}}
By default, the scope of the geo-spatial index assumption is -180~180, which is convenient for processing latitude and longitude, or it can be set to a range:
Db.map.ensureIndex ({"GPs": "2d"},{"min": -360, "Max": 360})
2, find
Look through the Find function:
The first 5 documents of the map collection will be returned in the form of a from near (40,80).
Db.map.find ({"GPs": {"$near": [40,80]}}). Limit (5)
To find through database commands:
Db.runcommand ({geonear: "Map", near:[40,80],num:5});
Geonear is similar to the Find normal lookup using $near, but Geonear also returns the distance from each document to the query point. The distance is in the form of inserting data, such as by latitude, and the distance is latitude and longitude.
For specific other spatial indexes, refer to the MongoDB official documentation for an introduction to the index.
V. Summary
For the use of indexes, you can use explain to view the effects of the current index to establish an appropriate index. If the index does not fit the current business requirements, modify or delete it, so that unnecessary or inappropriate indexes will have an impact on the database operation. After all, the index needs to consume memory and file storage. For indexes to be maintained in real time, do not index each key.