MongoDB Index 1

Source: Internet
Author: User

This blog is written after listening to the lesson-Getting started with MongoDB

When the amount of data is small, it can be queried quickly without using an index, but when the amount of data is large, the index is used to improve efficiency.

1.db.demo.getindexes () can have indexes contained in the table

You can see that the key's collection contains _id, which means that the default index in the table is _id

2.db.demo.ensureindex ({x:1})

X is 1 forward sort

X is-1 reverse sort

Note: When creating an index, the use of the database before the creation, can not be used when the creation of a serious impact on database performance, the general index after inserting data in the creation, may have an impact on the database write, but can be ignored.

When querying data using an index, performance can be significantly improved.

Indexes can be created repeatedly, and if they are created repeatedly, they are returned directly to success.

3,_id Use of indexes

He's a lot of collections. A unique index established by default

4. Single Key index

Db.demo1.ensureIndex ({x:1})

Value is a single value

5. Multi-Key indexing

Unlike a single key, a value has multiple records, such as an array, a collection, and so on.

When this data is crossed, MONGO will assume that X is a multi-key index.

6. Composite Index

Personal feeling is the index that is established for querying multiple property values. Nothing different.

7. Outdated indexes

Indexes that expire after a period of time, are deleted after they expire, are suitable for storing login information, logs, etc.

Db.demo1.ensureIndex ({qqq:1},{expireafterseconds:10})

The second parameter is the expiration time of the index, meaning that the inserted data is deleted after that time.

No more. have free time in more!

MongoDB Index 1

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.