mongodb index operation

    In mongodb, when the amount of data in a collection is very large, such as millions of pieces of data, if no index is used, the query of the data will perform a full table scan, and the query speed will be very slow at this time. At this point, we need to create an index for the collection to speed up the query. Since indexes can speed up our queries, should we build indexes on every field of the collection? This is obviously incorrect. Although the index can speed up our query, it will slow down when inserting, updating, and deleting data, because the index needs to be maintained at this time, so we rationally create an index on the collection.

 

Syntax for indexing:

db.collections.createIndex({

    key1 : 1, // Indicates that a positive-order index is created for key1

    key2 : -1 // Indicates that a flashback index is created for key2

} ,{

 background :true, // true : 表示后台创建索引 false:表示同步创建索引,会阻塞mongodb其余的操作

 unique : true , // true: means create a unique index  false: means non-unique. For hash indexes, this option has no effect.

 name : 'index_1', // name represents the name of the specified index, if not specified, mongodb will generate a name by default.

 partialFilterExpression :null, // 表示根据某些条件建立索引,比如:为name=5的字段建立索引

 sparse : true, // true: Indicates that a sparse index is established, indicating that the field is only indexed when the field exists. false: not a sparse index

 expireAfterSeconds : 10, //表示10秒后,该文档会被删除

 weights : null // Indicates the weight, the weight of the specified field in the full-text index, by default it is 1

 default_language:'zhs', // 全文索引分词的语言,默认情况下是英文

 ......

});

 

Default _id index:

Corresponding to the default _id index, mongodb will add a unique index to this field by default, and this index cannot be deleted.

 

Create a single key index:

db.persons.createIndex({"userId": 1}, {background:true,name:"index_01"});

    The above means to create a positive-order index on userId and create it in the background. The name of the index is index_01

 

Create a compound index:

db.persons.createIndex({"userId": 1,"age":-1}, {background:true,name:"index_02"})

    Note: 1. The above uses userId and age to create a composite index.

               2. Up to 31 fields can be combined into a composite index

               3. The order of fields when creating a composite index has an impact. For example, it is userId and then age. When querying data, the index can be used to query using userId, and the index can also be used to query using userId and age. Age and userId will not use the index to check.

               4. To summarize the above 3, the composite index of mongodb supports prefix search, that is, if the order of the fields to be queried is the prefix of the created field, the index can be used. For example: userId is the prefix of userId and age, then the index can be used.

 

Full-text index:

 

Geolocation Index:

 

Check out the index on the collection:

db.persons.getIndexes()

 Drop an index on a collection:

// delete the index whose index name is index_01
db.persons.dropIndex("index_01");
// delete all indexes     
db.persons.dropIndexes();

 Rebuild the index:

db.persons.reIndex()

    This operation deletes all indexes on this collection, and then rebuilds them.

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326044445&siteId=291194637