mongo 索引 命令 使用demo

数据存储

2017-07-11

246

0

技术:mongo3.2.0

运行环境:centos7 + mongo 3.2.0

demo功能:提供基于mongo命令的索引操作

ps:

1. 默认集合名字 hellokitty

查看某个集合全部索引

db.hellokitty.getIndexes()

 

创建普通索引

创建以age字段升序, name降序的索引

>db.hellokitty.ensureIndex({"age":1,"name":0})
##执行结果
> db.hellokitty.ensureIndex({"age":1,"name":0})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

查看索引结果

> db.hellokitty.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "db_dev.hellokitty"
        },
        {
                "v" : 1,
                "key" : {
                        "age" : 1,
                        "name" : 0
                },
                "name" : "age_1_name_0",
                "ns" : "db_dev.hellokitty"
        }
]

创建唯一索引

#创建以name字段的唯一索引
db.hellokitty.ensureIndex({"name":1},{"unique":true})

#创建结果
> db.hellokitty.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "db_dev.hellokitty"
        },
        {
                "v" : 1,
                "key" : {
                        "age" : 1,
                        "name" : 0
                },
                "name" : "age_1_name_0",
                "ns" : "db_dev.hellokitty"
        },
        {
                "v" : 1,##这个是刚刚创建的索引
                "unique" : true,
                "key" : {
                        "name" : 1
                },
                "name" : "name_1",
                "ns" : "db_dev.hellokitty"
        }
]

删除索引

#删除索引
db.hellokitty.dropIndex({"age":1,"name":0})

#删除结果
> db.hellokitty.dropIndex({"age":1,"name":0})
{ "nIndexesWas" : 3, "ok" : 1 }
>
> db.hellokitty.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "db_dev.hellokitty"
        },
        {
                "v" : 1,
                "unique" : true,
                "key" : {
                        "name" : 1
                },
                "name" : "name_1",
                "ns" : "db_dev.hellokitty"
        }
]

查看查询语句的执行过程-explain()

性能优化是的时候用到, 查询语句最好能够用上我们创建的索引, 否则索引不是白费了吗

#查询命中索引的情况
db.hellokitty.find({'name':'francis'}).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "db_dev.hellokitty",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "name" : {
                                "$eq" : "francis"
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "name" : 1
                                },
                                "indexName" : "name_1",
                                "isMultiKey" : false,
                                "isUnique" : true,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "name" : [
                                                "[\"francis\", \"francis\"]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "09f86f5efc82",
                "port" : 27017,
                "version" : "3.2.0",
                "gitVersion" : "45d947729a0315accb6d4f15a6b06be6d9c19fe7"
        },
        "ok" : 1
}

#插叙没有命中索引的情况
db.hellokitty.find({'age':12}).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "db_dev.hellokitty",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "age" : {
                                "$eq" : 12
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "age" : {
                                        "$eq" : 12
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "09f86f5efc82",
                "port" : 27017,
                "version" : "3.2.0",
                "gitVersion" : "45d947729a0315accb6d4f15a6b06be6d9c19fe7"
        },
        "ok" : 1
}

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think