mongodb中在嵌套子文檔的文檔上面建立索引

來源:互聯網
上載者:User

標籤:mongodb

  1. 在mongodb的test庫:

> 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"}});


2.對欄位 info 建立索引:

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


3.data表的索引查詢:

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.索引的用法:

以下查詢是可以用到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"});

可以使用query.explain()查看索引的使用:

    

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

}


但是這樣的查詢就不行:

>db.data.find({"info.city":"beijing"});    //欄位部分必須加引號

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

這樣的查詢語句,只能使用類似的複合式索引:

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


5.複合式索引

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

即使查詢時,與定義的排序相反,也是可以使用索引掃描的。

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

}


mongodb中在嵌套子文檔的文檔上面建立索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.