Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use Index in mongodb Database

Shulou Source: shulou.com Published: 2022-05-31 21:13:52 10月01日 Update

This article will explain in detail how to use the index in the mongodb database. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Indexes can accelerate data output in relational databases, as well as non-relational databases, which can reduce disk IO access and have a significant effect on large amounts of data. Currently, mongodb supports the Btel TreeReferenceUniqueParseQuery hash index.

Generate data in a mongodb cluster

[root@node2 mongodb-4.0.8] # / bin/mongo-- host 172.16.8.24-- port 27017

MongoDB shell version v4.0.8

Connecting to: mongodb://172.16.8.24:27017/?gssapiServiceName=mongodb

Implicit session: session {"id": UUID ("a1abbd3a-fe32-46ac-a959-4f8a62abd990")}

MongoDB server version: 4.0.8

Server has startup warnings:

Wuhan:PRIMARY > for (var item1 witch I db.stu.find (). Count ()

one thousand

Wuhan:PRIMARY > db.stu.find ()

{"_ id": ObjectId ("5ca3004015fc3dad4a419a75"), "sn": 1, "name": "student1"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a76"), "sn": 2, "name": "student2"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a77"), "sn": 3, "name": "student3"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a78"), "sn": 4, "name": "student4"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a79"), "sn": 5, "name": "student5"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a7a"), "sn": 6, "name": "student6"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a7b"), "sn": 7, "name": "student7"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a7c"), "sn": 8, "name": "student8"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a7d"), "sn": 9, "name": "student9"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a7e"), "sn": 10, "name": "student10"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a7f"), "sn": 11, "name": "student11"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a80"), "sn": 12, "name": "student12"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a81"), "sn": 13, "name": "student13"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a82"), "sn": 14, "name": "student14"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a83"), "sn": 15, "name": "student15"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a84"), "sn": 16, "name": "student16"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a85"), "sn": 17, "name": "student17"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a86"), "sn": 18, "name": "student18"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a87"), "sn": 19, "name": "student19"}

{"_ id": ObjectId ("5ca3004015fc3dad4a419a88"), "sn": 20, "name": "student20"}

Type "it" for more

Wuhan:PRIMARY >

I. use of B-Tree index

1. Single-column index use

Wuhan:PRIMARY > db.stu.ensureIndex ({sn:1})-- create an index on the sn field

Wuhan:PRIMARY > db.stu.find ({sn:50}). Explain ();-- check whether the execution plan is indexed

Wuhan:PRIMARY > db.stu.getIndexKeys ()-- see how many keys there are in the table

[{"_ id": 1}, {"sn": 1}]

Wuhan:PRIMARY > db.stu.getIndexes ()-- View all indexes of a table

[{"v": 2, "key": {"_ id": 1}, "name": "_ id_", "ns": "tong.stu"}, {"v": 2, "key": {"sn": 1}, "name": "sn_1", "ns": "tong.stu"}]

Wuhan:PRIMARY > db.stu.dropIndex ({sn:1});-- delete the index of the sn field

Wuhan:PRIMARY > db.stu.dropIndexes ();-- delete all indexes

two。 Use of multi-column indexes

Wuhan:PRIMARY > db.stu.ensureIndex ({name:1}, {name: "IX_name"})-create a multi-column index

{"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1, "operationTime": Timestamp (1554188377, 2), "$clusterTime": {"clusterTime": Timestamp (1554188377, 2), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}

Wuhan:PRIMARY > db.stu.getIndexes ()-- View index information

[{"v": 2, "key": {"_ id": 1}, "name": "_ id_", "ns": "tong.stu"}, {"v": 2, "key": {"name": 1}, "name": "IX_name", "ns": "tong.stu"}]

Wuhan:PRIMARY > db.stu.dropIndex ({name: "IX_name"})-- delete one of the indexes

{"operationTime": Timestamp (1554188499, 1), "ok": 0, "errmsg": "can't find index with key: {name:\" IX_name\ "}", "code": 27, "codeName": "IndexNotFound", "$clusterTime": {"clusterTime": Timestamp (1554188499, 1), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}

3. Subdocument index usage

Wuhan:PRIMARY > db.shop.insert ({name: "Nokia", spc: {weight:120,area: "taiwan"}})-- write data

WriteResult ({"nInserted": 1})

Wuhan:PRIMARY > db.shop.insert ({name: "sanxing", spc: {weight:100,area: "hanguo"}})-- write data

WriteResult ({"nInserted": 1})

Wuhan:PRIMARY > db.shop.find ()-- query data

{"_ id": ObjectId ("5ca337ff15fc3dad4a419e5d"), "name": "Nokia", "spc": {"weight": 120, "area": "taiwan"}}

{"_ id": ObjectId ("5ca3382c15fc3dad4a419e5e"), "name": "sanxing", "spc": {"weight": 100, "area": "hanguo"}}

Wuhan:PRIMARY > db.shop.find ({"spc.area": "hanguo"});-- query the data of the subdocument

{"_ id": ObjectId ("5ca3382c15fc3dad4a419e5e"), "name": "sanxing", "spc": {"weight": 100, "area": "hanguo"}}

Wuhan:PRIMARY > db.shop.ensureIndex ({"spc.area": 1});-- Sub-document creates index

{"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1, "operationTime": Timestamp (1554200928, 2), "$clusterTime": {"clusterTime": Timestamp (1554200928, 2), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}

Wuhan:PRIMARY > db.shop.getIndexes ()

[{"v": 2, "key": {"_ id": 1}, "name": "_ id_", "ns": "tong.shop"}, {"v": 2, "key": {"spc.area": 1}, "name": "spc.area_1", "ns": "tong.shop"}]

Wuhan:PRIMARY >

two。 Unique index (the field value in the unique index must be unique)

Wuhan:PRIMARY > db.stu.ensureIndex ({name:1}, {unique:true})

{"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1, "operationTime": Timestamp (1554188549, 2), "$clusterTime": {"clusterTime": Timestamp (1554188549, 2), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}

Wuhan:PRIMARY > db.stu.getIndexes ()

[{"v": 2, "key": {"_ id": 1}, "name": "_ id_", "ns": "tong.stu"}, {"v": 2, "unique": true, "key": {"name": 1}, "name": "name_1", "ns": "tong.stu"}]

Wuhan:PRIMARY > db.stu.totalIndexSize ()-- View the index size

40960

Wuhan:PRIMARY > db.stu.totalSize ()

69632

Wuhan:PRIMARY >

three。 Sparse index (create an index if a field has a value, no value without an index)

Wuhan:PRIMARY > db.shop.find ()

{"_ id": ObjectId ("5ca337ff15fc3dad4a419e5d"), "name": "Nokia", "spc": {"weight": 120, "area": "taiwan"}}

{"_ id": ObjectId ("5ca3382c15fc3dad4a419e5e"), "name": "sanxing", "spc": {"weight": 100, "area": "hanguo"}}

Wuhan:PRIMARY > db.shop.insert ({})-insert a null value

WriteResult ({"nInserted": 1})

Wuhan:PRIMARY > db.shop.find ()

{"_ id": ObjectId ("5ca337ff15fc3dad4a419e5d"), "name": "Nokia", "spc": {"weight": 120, "area": "taiwan"}}

{"_ id": ObjectId ("5ca3382c15fc3dad4a419e5e"), "name": "sanxing", "spc": {"weight": 100, "area": "hanguo"}}

{"_ id": ObjectId ("5ca3419c15fc3dad4a419e5f")}

Wuhan:PRIMARY > db.shop.ensureIndex ({name:1}, {sparse:true});-- create sparse index

{"createdCollectionAutomatically": false, "numIndexesBefore": 2, "numIndexesAfter": 3, "ok": 1, "operationTime": Timestamp (1554203093, 2), "$clusterTime": {"clusterTime": Timestamp (1554203093, 2), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}

Wuhan:PRIMARY > db.shop.find ({name: "null"});-- null value is not displayed

Wuhan:PRIMARY >

four。 Hash index

Wuhan:PRIMARY > db.t.ensureIndex ({a: "hashed"})

{"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1, "operationTime": Timestamp (1554203661, 2), "$clusterTime": {"clusterTime": Timestamp (1554203661, 2), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}

Wuhan:PRIMARY > db.t.find ({a: "25"}) .explain ()

{"queryPlanner": {"plannerVersion": 1, "namespace": "tong.t", "indexFilterSet": false, "parsedQuery": {"a": {"$eq": "25"}}, "winningPlan": {"stage": "FETCH", "filter": {"a": {"$eq": "25"}, "inputStage": {"stage": "IXSCAN" "keyPattern": {"a": "hashed"}, "indexName": "a_hashed"-- displayed as hash index "isMultiKey": false, "isUnique": false, "isSparse": false, "isPartial": false, "indexVersion": 2, "direction": "forward", "indexBounds": {"a": [7200060250846542811,7200060250846542811]]}}, "rejectedPlans": []}} "serverInfo": {"host": "node2", "port": 27017, "version": "4.0.8", "gitVersion": "9b00696ed75f65e1ebc8d635593bed79b290cfbb"}, "ok": 1, "operationTime": Timestamp (1554203690, 1), "$clusterTime": {"clusterTime": Timestamp (1554203690, 1), "signature": {"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong (0)}

Wuhan:PRIMARY >

five。 Index rebuilding (when the index is not efficient, you can consider rebuilding the index)

Wuhan:PRIMARY > db.t.reIndex ();-- rebuild the index of t-table

About how to use the index in the mongodb database to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Indexes data databases fields documents sparse content articles more knowledge articles queries good salient information size to large effect efficiency disk Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Redmi Linux NVidia