About MongoDB index management Details of index creation view and delete operations

  • 2020-10-23 21:15:47
  • OfStack

Index is the most effective means to improve the efficiency of query. Index is a special data structure. The index stores part of the data in a form of easy traversal (such as: 1 specific field or 1 group of field values). The index will sort the stored values according to the rule of 1, and the storage location of the index is in memory, which will retrieve the data from the index very quickly. Without indexes, MongoDB must scan every document in the collection, which is very inefficient, especially when the data volume is large.

1. Create/rebuild indexes

MongoDB creates new indexes using the ensureIndex() method. Existing indexes can be rebuilt using reIndex().

1.1 Create index ensureIndex()

MongoDB creates indexes using the ensureIndex() method.

Grammatical structure


db.COLLECTION_NAME.ensureIndex(keys[,options])
[

keys, the list of parameters to index. For example: {KEY:1}, where key represents the field name, 1 represents ascending sort, can also be used using the number -1 descending order.
options, an optional parameter, represents the setting to build an index. Optional values are as follows:
background, Boolean, index in the background so that the index does not prevent other database activities. The default is false.
unique, Boolean, create only one index. The default is false.
name, String, specifies the name of the index. If not specified, MongoDB generates a concatenation of the name and sort order of 1 index field.
dropDups, Boolean, when creating only one index, if there is a repeat deletion of the same index that occurs later, keep only the first index.
sparse, Boolean, does not enable indexing for field data that does not exist in the document. The default is false.
v, index version, version number of index.
weights, document, index weight values between 1 and 99,999, indicating the score weight of the index relative to other index fields.

]

For example, index the collection sites:


> db.sites.ensureIndex({name: 1, domain: -1})
{
 "createdCollectionAutomatically" : false,
 "numIndexesBefore" : 1,
 "numIndexesAfter" : 2,
 "ok" : 1
}

Note: The index was created using createIndex() before version 1.8, and the method was removed after version 1.8

1.2 Rebuild index reIndex()


db.COLLECTION_NAME.reIndex()

For example, rebuild all indexes of the collection sites:


> db.sites.reIndex()
{
 "nIndexesWas" : 2,
 "nIndexes" : 2,
 "indexes" : [
  {
  "key" : {
 "_id" : 1
  },
  "name" : "_id_",
 "ns" : "newDB.sites"
 },
 {
  "key" : {
 "name" : 1,
 "domain" : -1
  },
  "name" : "name_1_domain_-1",
  "ns" : "newDB.sites"
 }
 ],
 "ok" : 1
}

2. Look at the index

MongoDB provide a method to check the index information: getIndexes () method can be used to view the collection of all indexes, totalIndexSize () to see the total size of the set index, db. system. indexes. find () to check all index information in the database.

2.1 View the index getIndexes() in the collection


db.COLLECTION_NAME.getIndexes()

For example, look at the index in the collection sites:


>db.sites.getIndexes()
[
 {
 "v" : 1,
 "key" : {
  "_id" : 1
 },
 "name" : "_id_",
 "ns" : "newDB.sites"
 },
 {
 "v" : 1,
 "key" : {
  "name" : 1,
  "domain" : -1
 },
 "name" : "name_1_domain_-1",
 "ns" : "newDB.sites"
 }
]

2.2 View the index size in the collection totalIndexSize()


db.COLLECTION_NAME.totalIndexSize()

For example, look at the collection sites index size:


> db.sites.totalIndexSize()
16352

2.3 check the database all index db. system. indexes. find ()


db.system.indexes.find()

For example, all indexes of the current database:


> db.system.indexes.find()

3. Delete the index

If it is not in the required index, we can delete it. When you drop an index, you can drop one index in the collection, and you can drop all the indexes.

3.1 Delete the specified index dropIndex()


> db.sites.ensureIndex({name: 1, domain: -1})
{
 "createdCollectionAutomatically" : false,
 "numIndexesBefore" : 1,
 "numIndexesAfter" : 2,
 "ok" : 1
}
0

For example, delete the index named "name_1_domain_-1" in the collection sites:


> db.sites.ensureIndex({name: 1, domain: -1})
{
 "createdCollectionAutomatically" : false,
 "numIndexesBefore" : 1,
 "numIndexesAfter" : 2,
 "ok" : 1
}
1

3.3 Delete all indexes dropIndexes()


> db.sites.ensureIndex({name: 1, domain: -1})
{
 "createdCollectionAutomatically" : false,
 "numIndexesBefore" : 1,
 "numIndexesAfter" : 2,
 "ok" : 1
}
2

For example, delete all indexes in the collection sites:


> db.sites.dropIndexes()
{
 "nIndexesWas" : 1,
 "msg" : "non-_id indexes dropped for collection",
 "ok" : 1
}

The above content is MongoDB index management, including the creation of the index, view the index, delete the index of all aspects of the command and use methods, I hope you can help


Related articles: