Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A brief introduction to MongoDB Index

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "A brief introduction to MongoDB Index". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "A brief introduction to MongoDB Index".

Catalogue

Brief introduction to Index

1. Grammar preparation

two。 Data preparation:

3. Indexes

3.1 unique Index

3.2 single key index

3.3 Multi-key index

3.4 Composite index

3.5 Cross indexing

3.6 partial index

3.7 overlay index

3.8 full-text index

4. Index limit

4.1 query restrictions

4.2 scope limit

Brief introduction to Index

Index can improve the query speed of documents, but the process of indexing requires the use of computing and storage resources. Under the premise that the index has been established, inserting new documents will cause the rearrangement of the index order.

The index of MongoDB is based on B-tree data structure and corresponding algorithm. The tree index stores the values of a specific field or set of fields, sorted by field values. The sorting of index entries supports valid equation matching and range-based query operations.

1. Grammar preparation

Explain (): view the execution plan

GetIndexes (): view all indexes

Hint (): force an index to be used for query

two。 Data preparation:

{"_ id": ObjectId ("6127594238754d0067383ff6"), "xh": 1, "szly": {"lymc": "AA Building", "z": "A", "lh": "1", "sy": "what do you mean"}, "qtxx": {"nsssjg": "A, area" "sfwkgh": "No", "cylx": "Tertiary Industry", "rzlysj": "2011.11", "fwcqdw": "Henan Real Estate Co., Ltd.", "fwszlc": "5", "fjh": "601,604", "mj": 56, "cyry": 5, "yzj": 2 "qylx": "H, other", "ssqylx": "C, unlisted Enterprises", "lxr": "AA Blue", "lxdh": "85685685"}, "sssq": "Yuhong Building Community", "frxx": {"qyfr": "AA Blue", "qyfzr": "AA Blue" "lxfs": "18888888888"}, "qyjbxx": {"xy": "P Education", "qymc": "Zhengzhou Guancheng Hui training School", "gsyyzzh": "31313123", "swdjzh": "123123123", "tyxydm": "313123123", "zcdz": "6th floor of Block An of Yuhong International" "jjxz": "Limited liability company", "zczb": 100}, "importMonth": "202108", "batch": "162998706400.1", "createBy": "1", "department": "district government", "createTime": ISODate ("2021-08-26T09:05:06.416Z"), "status": 0 "ddly": {"zh": "," lh ":", "sy": "}," lcxx ": {" zb ":", "szlc": "", "mj": ""}, "updateBy": "1" "updateTime": "2021-08-27 11:14:31"} 3. Indexes

3.1 unique Index

  default index: _ id (unique index)

The   unique index ensures that the key corresponding to the index will not have the same value, and throws an exception if the field in which the unique index is located is written to duplicate data.

Db.getCollection ("qydrmb_copy") .createIndex ({"qtjbxx": 1}, {unique:true}) 3.2 single key index

  is the most common index and is not created automatically.

Example of creating a single index by  

Db.getCollection ("qydrmb_copy") .createIndex ({"qtjbxx": 1}) db.getCollection ("qydrmb_copy") .createIndex ({"qtxx.fwcqdw": 1})

Note:

Qydrmb_copy: collection name

Qtjbxx: collection field name

Qtxx.fwcqdw: collection child collection field name

1: sort in ascending order

-1: sort in descending order

3.3 Multi-key index

  multi-key indexes are created in the same way as single-key indexes, with the difference: the value of a field, which has multiple records, such as an array

Note: it is not possible to distinguish whether the index is a single-key index or a multi-key index through getIndexes (). It can be viewed in the print information of the explain () execution plan (isMultKey attribute).

3.4 Composite index

The   composite index jointly creates an index for multiple fields, sorting by the first field, documents with the same first field by the second field, and so on.

  syntax:

Db.collection_name.createIndex ({Index key name: collation, index key name: collation,.})

  composite index can satisfy more query scenarios than single-field index. It can satisfy not only the query combined by multiple fields, but also all the queries that match the index prefix.

Note: create a composite index A, B, the query condition contains A _ Magi B will walk the index, the query condition contains A, but also will go the index, there is no An in the query condition, only one B, at this time will not walk the index. An is called the index prefix.

3.5 Cross indexing

  cross-indexing is for multiple fields of a collection to be indexed separately, and multiple fields are used as query conditions when querying, which is called cross-indexing.

The difference between   cross-index and composite index: cross-index An and B are two indexes, and An and B in the composite index constitute an index.

Note: a collection contains cross indexes An and B, and cross indexes are triggered if the query condition contains A, or B, or A, B.

3.6 partial index

  partial indexes are indexed on documents that meet certain criteria, and version 3.2 only supports this feature.

The   MongoDB partial index creates an index only for documents in a collection that meet the specified filter criteria. The simple understanding is that some indexes are indexes with filter conditions, that is, indexes exist only on certain documents.

Syntax:

Db.collection_name.createIndex ({index key name: collation}, {partialFilterExpression: {"key name of previous sort": {matching condition: condition value})

Note: partial indexes and uniqueness are used together, and unique constraints apply only to documents that meet the filter criteria.

3.7 overlay index

1. All query fields are part of the index

two。 All fields returned by the query are in the same index

For example, if an index contains two fields An and B, the query condition has only one A, and the returned results are An and B, then the overwrite index will be triggered, that is, the entire document will no longer be scanned, but the data will be obtained directly from the index.

3.8 full-text index

  full-text retrieval establishes an index for each word, indicating the number and location of the word in the article. When the user queries, the retrieval program searches according to the pre-established index and feeds back the search results to the user's retrieval method.

  MongoDB supports full-text search since version 2.4, and currently supports full-text indexing in 15 languages (danish, dutch, english, finnish, french, german, hungarian, italian, norwegian, portuguese, romanian, russian, spanish, swedish, turkish).

Full-text search is enabled by default after   mongo2.6.

Create syntax:

Db.collection_name.ensureIndex ({Field name of full-text index: "text"})

Query syntax:

Db.collection_name.find ({$text: {$search: "retrieved value"}})

4. Index limit

4.1 query restrictions

The index cannot be used by the following query:

Regular expressions (except leftmost matching) and non-operators, such as $nin,$not, etc.

Arithmetic operators, such as $mod, etc.

You can use explain () to see if the index is executed

4.2 scope limit

Cannot have more than 64 indexes in the collection

The length of the index name cannot exceed 128 characters

A matching index can have up to 31 fields.

The size of the index cannot exceed the memory limit, and if the limit is exceeded, Mongo will delete some indexes, resulting in performance degradation.

At this point, I believe you have a deeper understanding of the "brief introduction to the MongoDB index". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report