Novice MongoDB Learning---(vi) MONGODB index

Source: Internet
Author: User
Tags net time

MongoDB Index


PS: About half a month the wood has been updated because of the previous phase of the business trip and these days suddenly come to the project. Causes the net time to reduce sharply, really is sorry, later anticipates will be better.


Indexes can often greatly improve the efficiency of queries. Assume there is no index. MongoDB must scan each file in the collection while reading the data and select those records that meet the query criteria.

Such a scan full set of query efficiency is very low, especially in the processing of large amounts of data, the query can take dozens of seconds or even a few minutes, which is very fatal to the performance of the site.

An index is a special data structure that is stored in a collection of data that is easily traversed for reading. An index is a structure that sorts the values of one or more columns in a database table


Ensureindex () method

MongoDB uses the Ensureindex () method to create an index.

Grammar

The basic syntax format for the Ensureindex () method is as seen below:

> DB . collection_name . Ensureindex ({KEY:1})

The Key value in the syntax is the index field you want to create, 1 for the specified index in ascending order, assuming that you want to create an index in descending order specified as-1.

Instance
> DB . MyCol . Ensureindex ({"title":1})  >

You can also set the Ensureindex () method to create an index using multiple fields (called composite indexes in a relational database).

> DB . MyCol . Ensureindex ({"title":1,"description":-1 })>

Ensureindex () receives an optional number of parameters. The list of optional references is as follows:

Parameter Type Description
Background Boolean The indexing process can clog other database operations, and background can specify a later way to create an index, that is, add a "background" optional number of references. The default value for "Background" is false.
Unique Boolean Whether the established index is unique.

Specifies true to create a unique index. The default value is false.

Name String The name of the index.

If not specified, MongoDB generates an index name by the field name and sort order of the connection index.

Dropdups Boolean Whether to delete the repeated records when establishing a unique index, specifying true to create a unique index. The default value is false.
Sparse Boolean Index is not enabled for field data that does not exist in the document. This number requires special attention. If set to true, documents that do not include the corresponding fields are not queried in the index field ... The default value is false.
Expireafterseconds Integer Specify a value in seconds, complete the TTL setting, and set the lifetime of the collection.
V Index version The version number of the index.

The default index version number depends on the version number that is executed when the index is created by Mongod.

Weights Document The index weight value. The value is between 1 and 99,999, indicating the score weight of the index relative to the other indexed fields.

Default_language String For a text index. This number determines the list of rules for the stop word and the stemming and the word changer. I feel English
Language_override String For a text index. This parameter specifies the name of the field that is included in the document. The language overrides the default language, and the default value is language.
Instance

To create an index in the background:

DB . Values . Ensureindex ({open:1,  close:1  },{background:true}) 

By adding the Background:true option when creating the index. Let the creation work run in the background.


Novice MongoDB Learning---(vi) MONGODB index

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.