Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB index management-create index, view index, delete index, rebuild

2025-03-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

First insert two records into the users collection, and then use the users collection to demonstrate index management:

> user1= {"name": "liming", "age": 20, "gender": "F"} {"name": "liming", "age": 20, "gender": "F"} > db.users.insert (user1) WriteResult ({"nInserted": 1}) > user2= {"name": "zhangsan", "age": 25, "gender": "F"} {"name": "zhangsan", "age": 25 "gender": "F"} > db.users.insert (user1) WriteResult ({"nInserted": 1}) > db.users.count () 2 create the index:

Mongodb uses the createIndex () and ensureIndex () methods to create indexes, the former for version 3.0 and above, and the latter for versions below 3.0.

Syntax:

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

Keys: a list of parameters to index. For example: {KEY:1}, where key represents the field name, 1 indicates ascending sort, or you can use the number-1 descending order.

Options: an optional parameter that represents the setting for indexing. Available values are as follows:

Background,Boolean, indexing in the background so that other database activity is not blocked when indexing. The default is false.

Unique,Boolean, create a unique index. The default is false.

Name,String, specifying the name of the index. If not specified, MongoDB generates an index field whose name is concatenated with the sort order.

PartialFilterExpression, document. If specified, MongoDB will only index records that satisfy the filter expression.

Sparse,Boolean, which does not enable indexing for field data that does not exist in the document. The default value is false.

ExpireAfterSeconds,integer, which specifies the expiration time of the index

StorageEngine,document, which allows users to configure the storage engine of the index

> db.users.createIndex ({"name": 1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1}

Example 2: create a reverse index for the name field

> db.users.createIndex ({"name":-1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 2, "numIndexesAfter": 3, "ok": 1}

Example 3: create a composite index for the name,age field

> db.users.createIndex ({"name": 1, "age": 1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 3, "numIndexesAfter": 4, "ok": 1}

Example 4: create an index for the age field in the background

> db.users.createIndex ({age:1}, {background:1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 4, "numIndexesAfter": 5, "ok": 1}

Reasons for creating indexes in the background:

During the creation of the index in the foreground, the database 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 servers that write frequently.

View the index:

The method provided by MongoDB to view index information:

The getIndexes () method can be used to view all indexes of the collection

The getIndexKeys () method looks at the index key.

TotalIndexSize () to view the total size of the collection index

The getIndexSpecs () method views the details of each index of the collection

Example 1: usage of getIndexes ()

> db.users.getIndexes () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "test1.users"}, {"v": 1, "key": {"name": 1} "name": "name_1", "ns": "test1.users"}, {"v": 1, "key": {"name":-1}, "name": "name_-1", "ns": "test1.users"}, {"v": 1 "key": {"name": 1, "age": 1}, "name": "name_1_age_1", "ns": "test1.users"}, {"v": 1, "key": {"age": 1} "name": "age_1", "ns": "test1.users", "background": 1}]

The usage of example 2:getIndexKeys ()

> db.users.getIndexKeys () [{"_ id": 1}, {"name": 1}, {"name":-1}, {"name": 1, "age": 1}, {"age": 1}]

The usage of example 3:totalIndexSize ()

> db.users.totalIndexSize () 81920

The usage of example 4:getIndexSpecs ()

> db.users.getIndexSpecs () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "test1.users"}, {"v": 1, "key": {"name": 1} "name": "name_1", "ns": "test1.users"}, {"v": 1, "key": {"name":-1}, "name": "name_-1", "ns": "test1.users"}, {"v": 1 "key": {"name": 1, "age": 1}, "name": "name_1_age_1", "ns": "test1.users"}, {"v": 1, "key": {"age": 1} "name": "age_1", "ns": "test1.users", "background": 1}] Delete the index:

We can delete an index that is no longer needed. Mongodb provides two ways to delete an index:

The dropIndex () method is used to delete the specified index

The dropIndexes () method is used to delete all indexes

The usage of example 1:dropIndex ()

> db.users.dropIndex ("name_1") {"nIndexesWas": 5, "ok": 1} > db.users.dropIndex ("name_1_age_1") {"nIndexesWas": 4, "ok": 1} > db.users.getIndexSpecs () [{"v": 1, "key": {"_ id": 1} "name": "_ id_", "ns": "test1.users"}, {"v": 1, "key": {"name":-1}, "name": "name_-1", "ns": "test1.users"}, {"v": 1 "key": {"age": 1}, "name": "age_1", "ns": "test1.users", "background": 1}]

We can see that the index of the name field and the combined index of the name and age fields are deleted

The usage of example 2:dropIndexes ()

> db.users.dropIndexes () {"nIndexesWas": 3, "msg": "non-_id indexes dropped for collection", "ok": 1} > db.users.getIndexSpecs () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_" "ns": "test1.users"}]

After using the dropIndexes () method, all the indexes we built before have been deleted

Index rebuild:

We deleted all the indexes of users before. Now we create a positive index on the name field, and then rebuild the reverse index on the name field. We can see that the index rebuilt is to delete the index of the previous name field and create a new index. Before the reconstruction, the name field still has only one index.

> db.users.createIndex ({name:1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1} > db.users.getIndexSpecs [{"v": 1, "key": {"_ id": 1}, "name": "_ id_" "ns": "test1.users"}, {"v": 1, "key": {"name": 1}, "name": "name_1", "ns": "test1.users"}] > db.users.reIndex ({name:-1}) {"nIndexesWas": 2, "nIndexes": 2 "indexes": [{"key": {"_ id": 1}, "name": "_ id_", "ns": "test1.users"}, {"key": {"name": 1} "name": "name_1", "ns": "test1.users"}], "ok": 1} > db.users.getIndexSpecs () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_" "ns": "test1.users"}, {"v": 1, "key": {"name": 1}, "name": "name_1", "ns": "test1.users"}]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report