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

Build an index on top of a document in a nested subdocument in mongodb

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

In the test library of mongodb:

> db.data.insert ({name: "1616", info: {url: "http://www.1616.net/",city:"beijing"}});

> db.data.insert ({name: "hao123", info: {url: "http://www.hao123.com/",city:"beijing"}});"

> db.data.insert ({name: "ll4la", info: {url: "http://www.114la.com/",city:"dongguan"}});"

two。 Create an index on the field info:

> db.data.ensureIndex ({info: 1})

Index query for 3.data table:

Rs0:PRIMARY > db.data.getIndexes ()

[

{

"v": 1

"key": {

"_ id": 1

}

"name": "_ id_"

"ns": "test.data"

}

{

"v": 1

"key": {

"info": 1

}

"name": "info_1"

"ns": "test.data"

}

]

4. The use of the index:

The following query can use the index of info:

> db.data.find ({info: {url: "http://www.1616.net/", city:" beijing "}})

> db.data.find ({info: {url: "http://www.1616.net/"}})

> db.data.find ({info: {city: "beijing"})

You can use query.explain () to view the use of the index:

Rs0:PRIMARY > db.data.find ({info: {city: "beijing"}}) .explain ()

{

"queryPlanner": {

"plannerVersion": 1

"namespace": "test.data"

"indexFilterSet": false

"parsedQuery": {

"info": {

"$eq": {

"city": "beijing"

}

}

}

"winningPlan": {

"stage": "FETCH"

"inputStage": {

"stage": "IXSCAN"

"keyPattern": {

"info": 1

}

"indexName": "info_1"

"isMultiKey": false

"isUnique": false

"isSparse": false

"isPartial": false

"indexVersion": 1

"direction": "forward"

"indexBounds": {

"info": [

"[{city:\" beijing\ "}, {city:\" beijing\ "}"

]

}

}

}

"rejectedPlans": []

}

"serverInfo": {

"host": "mycentos.WORKGROUP"

"port": 27017

"version": "3.2.8"

"gitVersion": "ed70e33130c977bda0024c125b56d159573dbaf0"

}

"ok": 1

}

But this kind of query doesn't work:

> db.data.find ({"info.city": "beijing"}); / / the fields must be in quotation marks

> db.data.find ({info.url: "..."})

For such query statements, only similar composite indexes can be used:

> db.data.ensureIndex ({"info.url": 1, "info.city": 1})

5. Combinatorial index

> db.data.ensureIndex ({"info.url": 1, "info.city": 1})

Index scanning can be used even when querying, contrary to the sort defined.

Rs0:PRIMARY > db.data.find ({"info.url": / http:*/i}) .sort ({"info.url":-1, "info.city":-1}) .explain ()

{

"queryPlanner": {

"plannerVersion": 1

"namespace": "test.data"

"indexFilterSet": false

"parsedQuery": {

"info.url": / http:*/i

}

"winningPlan": {

"stage": "FETCH"

"inputStage": {

"stage": "IXSCAN"

"filter": {

"info.url": / http:*/i

}

"keyPattern": {

"info.url": 1

"info.city": 1

}

"indexName": "info.url_1_info.city_1"

"isMultiKey": false

"isUnique": false

"isSparse": false

"isPartial": false

"indexVersion": 1

"direction": "backward"

"indexBounds": {

"info.url": [

"[/ http:*/i, / http:*/i]"

"({},\"\ "]"

]

"info.city": [

"[MaxKey, MinKey]"

]

}

}

}

"rejectedPlans": []

}

"serverInfo": {

"host": "mycentos.WORKGROUP"

"port": 27017

"version": "3.2.8"

"gitVersion": "ed70e33130c977bda0024c125b56d159573dbaf0"

}

"ok": 1

}

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