Primary Exploration of MongoDB-detailed description of Indexes

Source: Internet
Author: User
Tags mongodb documentation
I. Index operation indexes are generated to optimize the query speed. The indexes of MongoDB and other relational databases, such as MySQL and Oracle, are almost the same, their index optimization experience also applies to MongoDB. 1. Create an index in MongoDB through the ensureIndex operation. The following tests show how to use an index or not use a cable.

I. Index operation indexes are generated to optimize the query speed. The indexes of MongoDB and other relational databases, such as MySQL and Oracle, are almost the same, their index optimization experience also applies to MongoDB. 1. Create an index in MongoDB through the ensureIndex operation. The following tests show how to use an index or not use a cable.

I. Index operations

Indexes are generated to optimize the query speed. The indexes of MongoDB are almost the same as those of other relational databases, such as MySQL and Oracle. Their index optimization experience is also applicable to MongoDB.

1. Create an index

The ensureIndex operation is used to create an index in MongoDB. The following describes the performance differences between indexes and indexes. You can use the explain function to analyze the query performance.

Insert test data:


Queries without indexes: <喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140324/2014032409104154.jpg" alt = "\">

Query using indexes:


The preceding test shows that using the appropriate index search can greatly optimize the query efficiency.

2. delete an index


The metadata of the index is stored in the system. indexes set of each database. This set is a reserved set and cannot be inserted or deleted.
The operation can only be performed through ensureIndex or dropIndexes.

Ii. Unique Index

The unique index ensures that the specified key of each document in the SET has a unique value. The unique index created by MongoDB is as follows:

db.person.ensureIndex({"name":1},{"unique":true})

If no corresponding key exists, the index stores it as null. Therefore, if a unique index is created for a key but multiple documents without the index key are inserted, insertion fails because the document contains null values.

Iii. Composite Index

You can create a composite index by inputting multiple key-value documents for the first parameter of ensureIndex. Composite indexes are suitable for optimization of multi-condition queries. Generally, if an index contains N keys, it will be helpful for the first few keys. For example, an index {"a": 1, "B": 1, "c": 1 ,..., "z": 1} Actually has {"a": 1}, {"a": 1, "B": 1}, {"a": 1, "B": 1, "c": 1} and other indexes, but {"B": 1}, {"a": 1, "c ": 1} and other index queries will not be optimized.

Iv. geospatial Index

The geospatial index of MongoDB is used to process the query of plane coordinates. It is very suitable for finding N locations closest to the current location and is often used in LBS applications.

1. Create a spatial index

db.map.ensureIndex({"gps":"2d"})
The value of the "gps" key must be a pair of values in some form: An array containing two elements or an embedded document containing two keys (random key names), as shown below:
{"gps":[0,100]}{"gps":{"x":0,"y":30}}{"gps":{"latitude":0,"longitude":100}}
By default, the range of the expected geospatial index value is-180 ~ 180, which is very convenient for processing longitude and latitude. You can also set its range:
db.map.ensureIndex({"gps":"2d"},{"min":-360,"max":360})
2. Search

Find through the find function:

The first five documents of the map set are returned from the coordinate point (40, 80.

db.map.find({"gps":{"$near":[40,80]}}).limit(5)

Search through database commands:

db.runCommand({geoNear:"map",near:[40,80],num:5});
GeoNear is similar to find common search using $ near, but geoNear returns the distance from each document to the query point. The distance is measured in the unit of inserted data. For example, the distance is the longitude and latitude.

For details about how to search for other spatial indexes, refer to the index description in the official MongoDB documentation.

V. Summary

To create an appropriate index for the use of the index, you can use explain to view the effect of the current index. If the index is not suitable for the current business needs, modify or delete it to avoid the impact of unnecessary or inappropriate indexes on database operations. After all, indexes consume both memory and file storage. Indexes must be maintained in real time without indexing every key.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.