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