首先我们来了解索引,如果有基础的可以直接看最后面的操作。

可参照 DoNotStop 的CSDN 博客 ,全文地址请点击:

https://blog.csdn.net/u013725455/article/details/52037897

创建索引:

mongodb使用createIndex()和ensureIndex()方法来创建索引,前者用于3.0及以上版本,后者用于3.0以下版本。
语法:
db.COLLECTION_NAME.ensureIndex(keys[,options])
keys:要建立索引的参数列表。如:{KEY:1},其中key表示字段名,1表示升序排序,也可使用使用数字-1降序。
options:可选参数,表示建立索引的设置。可选值如下:
background,Boolean,在后台建立索引,以便建立索引时不阻止其他数据库活动。默认值为false。
unique,Boolean,创建唯一索引。默认值 false。
name,String,指定索引的名称。如果未指定,MongoDB会生成一个索引字段的名称和排序顺序串联。
partialFilterExpression, document.如果指定,MongoDB只会给满足过滤表达式的记录建立索引.
sparse,Boolean,对文档中不存在的字段数据不启用索引。默认值是 false。
expireAfterSeconds,integer,指定索引的过期时间
storageEngine,document,允许用户配置索引的存储引擎

> db.users.createIndex({"name":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

例2:给name字段创建倒序索引

> db.users.createIndex({"name":-1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}

例3:给name,age字段创建组合索引

> db.users.createIndex({"name":1,"age":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}

例4:在后台给age字段创建索引

> db.users.createIndex({age:1},{background:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 4,
"numIndexesAfter" : 5,
"ok" : 1
}

在后台创建索引的原因:
在前台创建索引期间会锁定数据库,会导致其它操作无法进行数据读写,在后台创建索引是,会定期释放写锁,从而保证其它操作的运行,但是后台操作会在耗时更长,尤其是在频繁进行写入的服务器上。

查看索引:

MongoDB提供的查看索引信息的方法:
getIndexes()方法可以用来查看集合的所有索引,
getIndexKeys()方法查看索引键。
totalIndexSize()查看集合索引的总大小,
getIndexSpecs()方法查看集合各索引的详细信息
例1: getIndexes()的用法

> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : -1
},
"name" : "name_-1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1,
"age" : 1
},
"name" : "name_1_age_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test1.users",
"background" : 1
}
]

例2:getIndexKeys()的用法

> db.users.getIndexKeys()
[
{
"_id" : 1
},
{
"name" : 1
},
{
"name" : -1
},
{
"name" : 1,
"age" : 1
},
{
"age" : 1
}
]

例3:totalIndexSize()的用法

> db.users.totalIndexSize()
81920

例4:getIndexSpecs()的用法

> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : -1
},
"name" : "name_-1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1,
"age" : 1
},
"name" : "name_1_age_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test1.users",
"background" : 1
}
]

删除索引:

不再需要的索引,我们可以将其删除,mongodb提供两种删除索引的方法:
dropIndex()方法用于删除指定的索引
dropIndexes()方法用于删除全部的索引
例1:dropIndex()的用法

> db.users.dropIndex("name_1")
{ "nIndexesWas" : 5, "ok" : 1 }
> db.users.dropIndex("name_1_age_1")
{ "nIndexesWas" : 4, "ok" : 1 }
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : -1
},
"name" : "name_-1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test1.users",
"background" : 1
}
]

我们可以看到,name字段的索引和name与age字段的组合索引皆被删除

例2:dropIndexes()的用法

> db.users.dropIndexes()
{
"nIndexesWas" : 3,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
}
]

在使用了dropIndexes()方法后,我们之前建的所有索引都被删除掉了

索引重建:

我们之前把users的索引全部删除了,现在在name字段上建立一个正序索引,然后在name字段上重建倒序索引,可以看到重建索引是把之前name字段的索引删掉再新建一个索引的,重建之前name字段还是只有一个索引.

> db.users.createIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
}
]
> db.users.reIndex({name:-1})
{
"nIndexesWas" : 2,
"nIndexes" : 2,
"indexes" : [
{
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
}
],
"ok" : 1
}
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
}
]

linux环境给mongodb创建索引

1.首先我们进入到linux mongodb安装目录的bin下面执行以下命令:

./mongo --host ip:端口

2.选择用户

use admin

3.选择库

use 库名

4.执行创建索引命令,等待创建索引。

db.表名.createIndex({"title":1})

5.创建完成后执行exit退出

linux环境给mongodb创建索引的更多相关文章

  1. 关于mongodb创建索引的一些经验总结(转)

    查看语句执行计划: explain() 在mongodb3+版本后输出格式发生改变: 详情参见:https://docs.mongodb.com/v3.0/reference/method/curso ...

  2. Linux环境安装mongodb

    介绍 上篇介绍了Linux环境下安装Node.js的步骤,紧接着来安装mongodb.另外,推荐我的另一篇 Windows下图文详解Mongodb安装及配置,先在Windows下熟悉下mongodb, ...

  3. MongoDB 创建索引的语法

    1.为普通字段添加索引,并且为索引命名 db.集合名.createIndex( {"字段名": 1 },{"name":'idx_字段名'}) 说明: (1)索 ...

  4. Mongodb 创建索引

    db.getCollection('ct_project').ensureIndex({'pro_code':1})  创建索引 db.getCollection('ct_project').ensu ...

  5. MongoDB 创建索引及其他

    索引 以提升查询速度 测试:插入十万条数据到数据库中 for(i=0;i<100000;i++){db.t255.insert({name:'test'+i,age:i})} db.t1.fin ...

  6. MongoDB学习(五)Linux环境安装MongoDB

    一.   下载 从http://www.mongodb.org/downloads地址中下载:mongodb-linux-x86_64-2.4.11.tar 二.  安装 1>设置mongoDB ...

  7. Linux环境下oracle创建和删除表空间及用户

    #su - oracle $ sqlplus /nolog SQL> connect / as sysdba --//创建临时表空间 create temporary tablespace te ...

  8. linux环境下vim创建java文件,并编译运行

    一.前提 安装Java 二.创建工作目录并编辑java文件 三.编译 四.运行

  9. linux环境下mongodb启动操作

    pkill mongod 进入mongo shell :运行 db.shutdownServer() 1.进入mongo的shell  : mongo --port 1008 2.进入bin目录下 m ...

随机推荐

  1. Codeforces Round #523 (Div. 2) D. TV Shows 模拟(多重集 先把所有区间加入多重集合)+贪心+二分

    题意:给出n个电视节目的起始和结束时间  并且租一台电视需要x +y*(b-a)  [a,b]为时段 问完整看完电视节目的最小花费是多少 思路:贪心的思想 情况1 如果新租一台电视的花费<=在空 ...

  2. Codeforces Round #534 (Div. 1)

    A 构造题 有一个44的方格 每次放入一个横向12或竖向2*1的方格 满了一行或一列就会消掉 求方案 不放最后一行 这样竖行就不会消 然后竖着的放前两行 横着的放第三行 循环放就可以啦 #includ ...

  3. 哈尔滨工程大学第十四届程序设计竞赛(同步赛)F 小帆帆走迷宫(dp)

    题目描述 小帆帆被困在一个 NxN 的方格矩阵迷宫,每个格子中都有一个整数 A[i][j].小帆帆从迷宫起点(左上角)格子 A[1][1]开始走,每一步可以向右或向下移动,目标是移动到迷宫的出口右下角 ...

  4. hdu 4542 "小明系列故事——未知剩余系" (反素数+DFS剪枝)

    传送门 参考资料: [1]:https://blog.csdn.net/acdreamers/article/details/25049767 题意: 输入两个数 type , k: ①type = ...

  5. 使用fork的str_cli函数

    void str_cli(FILE *fp, int sockfd) { pid_t pid; char sendline[MAXLINE], recvline[MAXLINE]; ) { /* ch ...

  6. Eclipse——如何设置代码字体大小

    eclipse默认字体太小,1920*1080下分辨不清楚,接下来介绍一下如何更改默认字体大小: 1.window-Preferences 2.General-Appearance-Colors an ...

  7. 为什么单片机中既有Flash又有EEPROM

    单片机运行时的数据都存在于RAM(随机存储器)中,在掉电后RAM 中的数据是无法保留的,那么怎样使数据在掉电后不丢失呢?这就需要使用EEPROM 或FLASHROM 等存储器来实现. 插播一段:ROM ...

  8. kaldi通用底层矩阵运算库——CBLAS

    matrix/cblas-wrappers.h 该头文件对CBLAS与CLAPACK的接口进行了简单的封装(将不同数据类型的多个接口封装为一个). 比如 cblas_scopy和cblas_dcopy ...

  9. proxysql系列 ~ 运维相关

    一 常用命令   //实时加载   load mysql servers to runtime; mysql_server   load mysql users to runtime; mysql_u ...

  10. return *this和return this有什么区别?

    return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 ). return this返回当前对象的地址(指向当前对象的指针). 转: ...