欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongdb添加索引

程序员文章站 2022-04-10 17:10:51
...
1、cd 到mongdb目录
2、bin/mongo
3、use event
4、db.ogg_event_current.ensureIndex({"alertKey":-1})
5、db.ogg_event_current.ensureIndex({"severity":1})

[root@localhost ~]# cd /opt/mongodb-linux-x86_64-rhel70-3.2.9/
[root@localhost mongodb-linux-x86_64-rhel70-3.2.9]# bin/mongo
MongoDB shell version: 3.2.9
connecting to: test
Server has startup warnings: 
2017-09-14T16:45:11.375+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-09-14T16:45:11.375+0800 I CONTROL  [initandlisten] 
2017-09-14T16:45:11.376+0800 I CONTROL  [initandlisten] 
2017-09-14T16:45:11.376+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-09-14T16:45:11.376+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-14T16:45:11.376+0800 I CONTROL  [initandlisten] 
2017-09-14T16:45:11.376+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-09-14T16:45:11.376+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-14T16:45:11.376+0800 I CONTROL  [initandlisten]

切换到对应的collections
> show dbs
databaseName  0.000GB
event         0.233GB
local         0.000GB
test          0.000GB
> use event
switched to db event

查看当前数据库
> db
event

查看表
> show collections
default_event_exception
groovy_script
informatica_event_current
informatica_event_history
ogg_event_current
ogg_event_history
raw_message
summary_chinese_match


新建索引
> db.ogg_event_current.ensureIndex({"alertKey":-1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}
> db.ogg_event_current.ensureIndex({"severity":1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 3,
        "numIndexesAfter" : 4,
        "ok" : 1
}


查看索引
> db.ogg_event_current.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "event.ogg_event_current"
        },
        {
                "v" : 1,
                "key" : {
                        "alertKey" : -1
                },
                "name" : "alertKey_-1",
                "ns" : "event.ogg_event_current"
        },
        {
                "v" : 1,
                "key" : {
                        "alertKey" : 1
                },
                "name" : "alertKey_1",
                "ns" : "event.ogg_event_current"
        },
        {
                "v" : 1,
                "key" : {
                        "severity" : 1
                },
                "name" : "severity_1",
                "ns" : "event.ogg_event_current"
        }
]
相关标签: mongdb 索引