Introduction to MongoDb indexes and mongodb Indexes

Source: Internet
Author: User

Introduction to MongoDb indexes and mongodb Indexes

MongoDb Index

1. _ Id index: Default Index

2. single-Key Index: for example, a record: {x: 1, y: 2, z: 3}, statement db. collection. ensureIndex ({x: 1}) indicates creating an ascending index for x. The value 1 after x: 1 indicates ascending, and-1 indicates descending.

3. Multi-key index: the multi-key index is created in the same way as the single-key index. The difference lies in the field value. The value may have multiple records, for example, array. {x: [, 5]}

4. Composite Index: when there are more than one query condition, a composite index is required. For example, for a record {x: 1, y: 2, z: 3}, the statement db. collection. ensureIndex ({x: 1, y: 1 })

5. expired index: an index that will expire after a period of time. After the index expires, the corresponding data will be deleted, which is suitable for storing data that will expire after a period of time, such as user login information and stored logs. For example, insert a record db. collection. insert ({time: new Date ()}) indicates to insert data of the current time, db. collection. ensureIndex ({time: 1 },{ expireAfterSeconds: 10}) to create an expired index. Data in the time field will be deleted after 10, but this 10 s is not a definite value, because the mongodb deletion process is detected once every minute, when the set expireAfterSeconds is less than one minute, it will be deleted one minute later.

6. Full-text index: Creates full-text searchable indexes for strings and string arrays. Applicable to: {author: "", title: "", article: ""}, author indicates the author, title indicates the title, and article indicates the content.

Example: db. collection. ensureIndex ({key: "text"}), similar to the previous single-key index, the difference is that the key of the single-key index is the field name, the value is 1 or-1, here, the key is the same as the field name, but the value is a fixed text string.

To create a full-text index for multiple fields, db. collection. ensureIndex ({key_1: "text", key_2: "text"}) is similar to the previous multi-key index.

If you want to create an index for all fields, you can use db. collection. ensureIndex ({"$ **": "text"}) indicates that the values of all fields are indexed.

Note: Only one full-text index is allowed. You do not need to write the key for full-text index query. The key is the key we write in the index.

Use full-text index for query: db. collection. find ({$ text: {$ search: "aaa"}) indicates that the queried data contains aaa data. If there are multiple data records, multiple results are returned.

Db. collection. find ({$ text :{$ search: "aa bb cc dd"}), which indicates that the query data contains aa, bb, cc, or dd data, if multiple data entries exist, multiple results are returned. Here, space indicates or.

Db. collection. find ({$ text :{$ search: "aa bb-cc"}), which indicates that the query data contains aa or bb but does not contain cc Data, if there are multiple entries, multiple entries are returned. The symbols here indicate non.

Db. collection. find ({$ text :{$ search: "\" aa \ "\" bb \ "\" cc \""}}), indicates that the queried data contains aa, bb, and cc. If there are multiple entries, multiple entries are returned. Here, \ "actually indicates" because there is a layer outside ", when we use ", we need to add the Escape Character \. Here, the quotation marks indicate and.

7. full-text index similarity: This is for full-text indexing. We usually search for keywords in Baidu, mongodb has helped us take this into account in mongodb queries. $ Meta OPERATOR: {score: {$ meta: "textScore"}. The similarity of the query results can be returned after the query, Which is used with sort, it can be sorted by similarity.

Here we use the first query of the full-text index as an example: db. colletion. find ({$ text :{$ search: "aaa" }}, score: {$ meta: "textScore "}), the query result contains a similarity field. We can sort the data based on the similarity. Example: db. collection. find ($ text: {$ search: "aaa"}, score: {$ meta: "textScore "}). sort (score: {$ meta: "textScore"}), so that we can sort by similarity.

8. Full-text index restrictions: tip1. only one $ text query can be specified for each query.

Tip2. $ text query cannot appear in $ nor Query

Tip3. if the query contains $ text, hint no longer works.

Full-text indexes earlier than tip4. \ 3.2 do not support Chinese characters, and later versions start to support Chinese characters.

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.