mongodb中在嵌套子文档的文档上面建立索引-创新互联

  1. 在mongodb的test库:

    我们拥有十载网页设计和网站建设经验,从网站策划到网站制作,我们的网页设计师为您提供的解决方案。为企业提供网站设计制作、网站设计、微信开发、小程序定制开发、手机网站开发、H5开发、等业务。无论您有什么样的网站设计或者设计方案要求,我们都将富于创造性的提供专业设计服务并满足您的需求。

> 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

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:mongodb中在嵌套子文档的文档上面建立索引-创新互联
链接URL:http://cdiso.cn/article/gdseg.html

其他资讯