Daily maintenance operation of mongodb index
Create an index:
Db.t_order_detail.createIndex ({"order_id": 1})
Composite index:
Db.t_order_detail.createIndex ({"order_id": 1, "detail_id": 1, "batch_id": 1})
Create an index in the background:
Db.t_order_detail.createIndex ({order_id:1}, {background:1})
Create a TTL index:
a. After the specified time interval, the collection is invalidated:
Db.t_order_detail.createIndex ({"createTime": 1}, {expireAfterSeconds: 60: 60})-Expiration time (in seconds) is viewed using getIndexes ()
Modify the value of the expireAfterSeconds attribute of the TTL index:
Db.runCommand ({collMod: "t_order_detail", index: {keyPattern: {createTime: 1}, expireAfterSeconds: 7200}})
b. The specified time point expires and the collection expires:
Db.t_order_detail.createIndex ({"expireAt": 1}, {expireAfterSeconds:0})
Db.t_order_detail.insert ({
"createdAt": new Date ('Oct 21, 2018 21 Oct 3000')
"log_Event": 1
"log_Message": "Success!"
})
View the index:
Db.t_order_detail.getIndexes ()
View the index keys:
Db.t_order_detail.getIndexKeys ()
View the total index size of the collection:
Db.t_order_detail.totalIndexSize ()
View the details of each index of the collection:
Db.t_order_detail.getIndexSpecs ()
Delete the index:
Db.t_order_detail.dropIndex ("index_name")
Delete all indexes
The db.t_order_detail.dropIndexes () method is used to delete all indexes
Index rebuild:
Db.t_order_detail.reIndex ({"order_id": 1})
Note:
During the creation of the index in the foreground, the collection will be locked, which will prevent other operations from reading and writing. When the index is created in the background, the write lock will be released periodically to ensure the operation of other operations, but the background operation will take longer, especially on collections that are written frequently.