MongoDB Learning Note 04

Source: Internet
Author: User
Tags custom name

Create an index using the Ensureindex method, for the same collection, the same index needs to be created once, and repeated creation is futile.

The index of a key accelerates the query for that key, however, it may not help for other queries, even if the query contains the indexed key. Practice proves that you must create an index of all the keys used in the query

In general, if an index contains n keys, queries for the first few keys will help. If there is an index {"a": 1, "B": 1, "C": 1}, there is actually {"a": 1}, {"A": 1, "B": 1}, {"A": 1, "B": 1, "C": 1}, etc. However, queries with indexes such as {"B": 1}, {"A": 1, "C": 1} are not optimized, and only queries using the front of the index can use the index.

The query optimizer of MongoDB will rearrange the order of the query items in order to take advantage of the index

Sometimes, the most effective method is not to use an index, generally speaking, if the query is to return more than half of the results in the collection, it is more efficient to use a table scan than almost every document to look up the index.

The following questions should be considered when building an index:

1. What kind of query do you make? Which of these keys need to be indexed?

2. What is the index direction of each key?

3. How do I deal with extensions? Is there a different kind of key arrangement that keeps common data more in memory?

There is no difference between indexing the key of an embedded document and creating an index on a normal key.

As the collection grows, it needs to be indexed for a large number of sorts in the query, and if a key call to Sort,mongodb without an index needs to extract all the data into memory to sort, there is an upper limit to what can be done without indexing, and if the collection is too large to be sorted in memory, MongoDB will error. Indexed by sort to let MongoDB extract data sequentially, so that large-scale data can be sorted.

Each index in the collection has a string type name that uniquely identifies the index, which the server uses to remove the index or manipulate the index, by default the index name is similar to Keyname1_dir1_keyname2_dir2_..._keynamen_ Dirn this form, where Keynamex represents the key of the index, DirX represents the direction of the index (1 or-1), and the index name has a character limit, so a particularly complex index must use a custom name when it is created.

Creates a unique index, and if there is no corresponding key, the index stores it as a null, so if a unique index is established on a key, but multiple documents with that index key are inserted, the insert fails because the document contains a null value.

Explain returns the index used by the query (if any), time-consuming, and the number of scanned documents statistics "3.0 didn't see it."

The query optimizer of MongoDB chooses which index to use, the first time a query will try a variety of query scenarios, the first completed is determined to use, the others are terminated.

The query scheme is recorded for future queries against the same key, and the query optimizer periodically retries other scenarios in case the previous scenario is no longer optimal after adding new data.

If you find that MongoDB uses an unintended index, you can use hit to force an index.

The System.indexes collection contains detailed information about each index, and the collection finds that each collection has at least two documents corresponding to it, a collection itself, a corresponding collection containing indexes, and, for a collection of only standard "_id" indexes, "the length of the collection name cannot exceed 121 bytes, The collection name and index name cannot be combined by more than 127 bytes "

Indexing is time-consuming and laborious, and requires a lot of resources, using the {"Background": true} option to make the process complete in the background while the request is processed normally, and if the background option is not included, the database blocks all requests during indexing. Blocking practices make indexing faster, and creating indexes in the background can also add loads that do not cause the server to go down. Creating an index for an existing document is a little faster than creating an index before inserting all the documents.

Find the nearest N places to the current location, MongoDB provides a specialized index for coordinate plane queries, called a geospatial index. The index is also created using Ensureindex, except that the parameter is not 1 or-1, but rather "2d"

The value of the "GPs" key must be a pair of values in some form: an array of two elements or an inline document containing two keys

Geospatial queries are done in two ways, common query (find) or using database commands

MongoDB not only finds documents close to one point, but also finds documents within a specified shape, replacing the original "$near" with "$within"

To create a composite geo-spatial index

Db.map.ensureIndex ({"Location": "2d", "desc": 1})

MongoDB Learning Note 04

Related Article

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.