索引
自动创建和手工创建

db.stu.drop();
db.stu.insert({"name":"张三","sex":"男","age":18,"score":70,"address":"河南"});
db.stu.insert({"name":"李四","sex":"女","age":20,"score":60,"address":"山东"});
db.stu.insert({"name":"王五","sex":"男","age":17,"score":44,"address":"江苏"});
db.stu.insert({"name":"赵六","sex":"男","age":21,"score":80,"address":"山东"});
db.stu.insert({"name":"孙七","sex":"女","age":23,"score":50,"address":"湖北"});
db.stu.insert({"name":"tom","sex":"男","age":24,"score":20,"address":"海南"});
db.stu.insert({"name":"lucy","sex":"女","age":21,"score":62,"address":"浙江"});
db.stu.insert({"name":"jack","sex":"男","age":20,"score":90,"address":"美国"});
db.stu.insert({"name":"smith","sex":"男","age":19,"score":88,"address":"美国"});

查询默认状态下的stu集合索引内容
db.stu.getIndexes();

> db.stu.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.stu"
}
] v:索引版本
_id:1 表示升序

索引创建
db.集合.ensureIndex({列:1})
1表示升序 -1降序
db.stu.ensureIndex({"age":-1});

> db.stu.ensureIndex({"age":-1});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
db.stu.getIndexes();
> db.stu.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.stu"
},
{
"v" : 2,
"key" : {
"age" : -1
},
"name" : "age_-1",
"ns" : "test.stu"
}
]

这时索引名是自动命名的。命名规范: 字段名称_索引排序模式
索引使用分析

db.stu.find({"age":21}).explain();
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.stu",
"indexFilterSet" : false,
"parsedQuery" : {
"age" : {
"$eq" : 21
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"age" : -1
},
"indexName" : "age_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"age" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"age" : [
"[21.0, 21.0]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "centos1",
"port" : 27000,
"version" : "3.4.4",
"gitVersion" : "888390515874a9debd1b6c5d36559ca86b44babd"
},
"ok" : 1
}

"stage" : "IXSCAN"

在非索引的列上

> db.stu.find({"score":{"$gt":60}}).explain();
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.stu",
"indexFilterSet" : false,
"parsedQuery" : {
"score" : {
"$gt" : 60
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"score" : {
"$gt" : 60
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "centos1",
"port" : 27000,
"version" : "3.4.4",
"gitVersion" : "888390515874a9debd1b6c5d36559ca86b44babd"
},
"ok" : 1
}

"stage" : "COLLSCAN"

db.stu.find({"$or":[
{"age":{"$gt":21}},
{"score":{"$gt":60}}
]}).explain();

此时age上有索引,score上没有,使用的是全表扫描
"stage" : "COLLSCAN",

这时可以使用符合索引
db.stu.ensureIndex({"age":-1,"score":-1},{"name":"age_-1_score_-1"})

执行查询
db.stu.find({"$or":[
{"age":{"$gt":21}},
{"score":{"$gt":60}}
]}).explain(); "stage" : "COLLSCAN",
db.stu.find({"$or":[
{"age":21},
{"score":80}
]}).explain();
依然
"stage" : "COLLSCAN"

强制使用索引

db.stu.find({"$or":[
{"age":21},
{"score":80}
]}).hint({"age":-1,"score":-1}).explain(); "stage" : "IXSCAN"
db.stu.find({"$or":[
{"age":21},
{"score":80}
]}).hint({"age":-1,"score":1}).explain();
会报错
db.stu.find({"$or":[
{"age":{"$gt":21}},
{"score":{"$gt":60}}
]}).hint({"age":-1,"score":-1}).explain(); stage" : "IXSCAN"

删除索引
db.stu.dropIndex({"age":-1,"score":-1});
删除全部索引(除了_id外)
db.stu.dropIndexes();

 唯一索引

db.stus.ensureIndex({"name":},{"unique":true});
> db.stu.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "hk.stu"
}
] db.stu.insert({"name":"smith","sex":"男","age":19,"score":88,"address":"美国"});
> db.stu.ensureIndex({"name":1},{"unique":true});
{
"ok" : 0,
"errmsg" : "E11000 duplicate key error collection: hk.stu index: name_1 dup key: { : \"smith\" }",
"code" : 11000,
"codeName" : "DuplicateKey"
}
>
db.stu.remove({ "_id" : ObjectId("5943194f2e32953979ce4b4d")});
WriteResult({ "nRemoved" : 1 })
> db.stu.ensureIndex({"name":1},{"unique":true});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
} > db.stu.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "hk.stu"
},
{
"v" : 2,
"unique" : true,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "hk.stu"
}
]

过期索引 信息若干时间后过期

db.phones.ensureIndex({"time":1},{expireAfterSeconds:10});
//设置索引在10s后过期
db.phones.insert({"tel":"18600880451","code":"111","time":new Date()});
db.phones.insert({"tel":"18600880452","code":"112","time":new Date()});
db.phones.insert({"tel":"18600880453","code":"113","time":new Date()});
db.phones.insert({"tel":"18600880454","code":"114","time":new Date()});
db.phones.insert({"tel":"18600880455","code":"115","time":new Date()});
10秒后(不准确)
db.phones.find()
数据会消失

 全文索引

设置全文检索(必须先有数据,后加索引,drop后必须重建索引)

实现模糊查询
表示全文索引 $text 判断符
数据的查询使用$search 运算符
.查询指定关键字: {"$search":"查询关键字"}
.查询多个关键字(或关系):{"$search":"关键字1 关键字2 ..."}
.查询多个关键字(与关系):{"$search":"\"关键字1\" \"关键字2\" ..."}
.查询多个关键词(排除某一个):{"$search":"关键词1 关键词2 .... -关键词3"}
var data=[
{"title":"apple","con":"this fruit is apple"},
{"title":"pear","con":"this fruit is pear"},
{"title":"peach","con":"this fruit is peach"},
{"title":"apple pear","con":"two fruit"},
{"title":"pear apple peach","con":"three fruit"},
{"title":"pear,apple,peach","con":"three fruit"},
{"title":"apple_1","con":"fruit"},
];
db.fruit.insert(data);
db.fruit.ensureIndex({"title":"text"});

多个能设置权重
{"weights":{"title":2,"con":1}

查询title里含有apple的
db.fruit.find({"$text":{"$search":"apple"}})
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca7"), "title" : "pear apple peach", "con" : "three fruit" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca6"), "title" : "apple pear", "con" : "two fruit" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca8"), "title" : "pear,apple,peach", "con" : "three fruit" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca3"), "title" : "apple", "con" : "this fruit is apple" }
查询有apple或者pear
db.fruit.find({"$text":{"$search":"apple pear"}})
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca4"), "title" : "pear", "con" : "this fruit is pear" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca3"), "title" : "apple", "con" : "this fruit is apple" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca8"), "title" : "pear,apple,peach", "con" : "three fruit" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca6"), "title" : "apple pear", "con" : "two fruit" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca7"), "title" : "pear apple peach", "con" : "three fruit" }
查询有apple而且pear的
db.fruit.find({"$text":{"$search":"\"apple\" \"pear\""}})
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca8"), "title" : "pear,apple,peach", "con" : "three fruit" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca6"), "title" : "apple pear", "con" : "two fruit" }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca7"), "title" : "pear apple peach", "con" : "three fruit" }
查询有apple pear没有peach的
db.fruit.find({"$text":{"$search":"\"apple\" \"pear\" -peach"}})
{ "_id" : ObjectId("59445bfd3bdbe9486ee00ca0"), "title" : "apple pear", "con" : "two fruit" }

还可以使用相似度的打分来判断检索成果
为结果打分,分越高越准确

db.fruit.find({"$text":{"$search":"apple pear"}},{"score":{"$meta":"textScore"}})
排序
db.fruit.find({"$text":{"$search":"apple pear"}},{"score":{"$meta":"textScore"}}).sort({"score":{"$meta":"textScore"}})
> db.fruit.find({"$text":{"$search":"apple pear"}},{"score":{"$meta":"textScore"}}).sort({"score":{"$meta":"textScore"}})
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca6"), "title" : "apple pear", "con" : "two fruit", "score" : 1.5 }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca7"), "title" : "pear apple peach", "con" : "three fruit", "score" : 1.3333333333333333 }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca8"), "title" : "pear,apple,peach", "con" : "three fruit", "score" : 1.3333333333333333 }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca4"), "title" : "pear", "con" : "this fruit is pear", "score" : 1.1 }
{ "_id" : ObjectId("59445eb93bdbe9486ee00ca3"), "title" : "apple", "con" : "this fruit is apple", "score" : }

全文索引的限制:
一个集合只能创建一个全文索引,每次查询只能指定一个$text查询
$text不能出现在$nor查询中
查询如果包含了$text,$hint不起作用
不支持中文全文检索

地理信息索引

1.2d平面索引
2.2dsphere球面索引
2d 坐标保存的就是经纬度坐标

例子:定义一个商铺集合
db.shop.insert({"loc":[,]});
db.shop.insert({"loc":[,]});
db.shop.insert({"loc":[,]});
db.shop.insert({"loc":[,]});
db.shop.insert({"loc":[,]});
db.shop.insert({"loc":[,]});
为shop添加2d索引
db.shop.ensureIndex({"loc":"2d"});
2种查询方式
$near 查询距离某个点最近的坐标点
$geoWithin 查询某个形状内的点

db.shop.find({"loc":{"$near":[,]}})
> db.shop.find({"loc":{"$near":[,]}})
默认前100个
{ "_id" : ObjectId("5944748d2e861d522342cb87"), "loc" : [ , ] }
{ "_id" : ObjectId("5944748d2e861d522342cb86"), "loc" : [ , ] }
{ "_id" : ObjectId("5944748d2e861d522342cb88"), "loc" : [ , ] }
{ "_id" : ObjectId("5944748d2e861d522342cb89"), "loc" : [ , ] }
{ "_id" : ObjectId("5944748d2e861d522342cb8a"), "loc" : [ , ] }
{ "_id" : ObjectId("5944748e2e861d522342cb8b"), "loc" : [ , ] }
设置范围3个点内的
db.shop.find({"loc":{"$near":[,],"$maxDistance":}})
> db.shop.find({"loc":{"$near":[,],"$maxDistance":}})
{ "_id" : ObjectId("5944748d2e861d522342cb87"), "loc" : [ , ] }
{ "_id" : ObjectId("5944748d2e861d522342cb86"), "loc" : [ , ] }

在2d索引里支持最大距离,不支持最小距离
设置查询范围
使用 $geoWithin
矩形范围 {"$box":[[x1,y1],[x2,y2]]}
圆形范围 {"$center":[[x1,y1],r]}
多边形 {"$polygon":[[x1,y1],[x2,y2],[x3,y3] .....]}

.
db.shop.find({"loc":{"$geoWithin":{"$box":[[,],[,]]}}})
{ "_id" : ObjectId("5944748d2e861d522342cb87"), "loc" : [ , ] }
.
db.shop.find({"loc":{"$geoWithin":{"$center":[[,],]}}})
{ "_id" : ObjectId("5944748d2e861d522342cb86"), "loc" : [ , ] }
{ "_id" : ObjectId("5944748d2e861d522342cb87"), "loc" : [ , ] }
runCommand() 执行特定命令
db.runCommand({"geoNear":"shop",near:[,],maxDistance:,num:})
{
"results" : [
{
"dis" : ,
"obj" : {
"_id" : ObjectId("5944748d2e861d522342cb86"),
"loc" : [
, ]
}
},
{
"dis" : ,
"obj" : {
"_id" : ObjectId("5944748d2e861d522342cb87"),
"loc" : [
, ]
}
}
],
"stats" : {
"nscanned" : ,
"objectsLoaded" : ,
"avgDistance" : 0.5,
"maxDistance" : ,
"time" :
},
"ok" :
}

mongo索引的更多相关文章

  1. mongo 索引 安全、备份与恢复

    一.索引 创建大量数据 for(i=0;i<100000;i++){ db.t1.insert({name:"test"+i,age:i}) } 数据查找性能分析 db.t1 ...

  2. 使用Mongo索引需要注意的几个点

    1.正则表达式和取反运算符不适合建立索引 正则表达式:$regex 取反运算符:$ne ,$nin 2.backgroud建立索引速度缓慢 前台创建是会有阻塞,backgroud效率缓慢,实际情况实际 ...

  3. mongo: 索引

    索引创建 在学习索引之前,我们先看一下,如果没有添加索引时,我们用explain()函数,查看查询计划是什么样的. 发现使用的是BasicCursor,那么就代表我们没有索引,当我们查某一个数据的时候 ...

  4. mongo索引命令

    http://blog.csdn.net/salmonellavaccine/article/details/53907535 1. 创建/重建索引 MongoDB全新创建索引使用ensureInde ...

  5. mongo 索引,速度

    (如有打扰,请忽略)阿里云ECS大羊群,2U4G低至1.4折,限实名新用户,需要的点吧https://promotion.aliyun.com/ntms/act/vm/aliyun-group/tea ...

  6. Mongo索引学习笔记

    索引使用场景 优:加快查询速度 劣:增删改会产生额外的开销.占用空间 tips: 返回集合中一半以上的数据,全表扫描的效率高 索引基础 基础操作 查看索引:db.test.getIndexes() 创 ...

  7. mongo索引(转)

    转自  :https://www.cnblogs.com/efforts-will-be-lucky/p/7324789.html 默认索引 对于每一个集合(除了capped集合),默认会在_id字段 ...

  8. Mongo Index

    摘要 mongo 的索引非常强大,和关系型数据库索引没什么区别.这里主要介绍本人在mongo索引上的犯的错. 索引种类 1.单字段索引 2.复合索引 多个字段索引 如{name:1,address:1 ...

  9. mongo学习笔记2--索引及表设计

    -背景: 鉴于我们使用mongo作为数据库,期间少不了需要添加索引和对业务表进行设计.因此以下我对mongo索引及表设计原则做了一些分享.希望对大家有用,如有错误还望指正~ MongDB的索引类型简介 ...

随机推荐

  1. OpenID Connect 是什么?

    一.OpenID Connect的概念 1.OpenID Connect 是什么? OpenID Connect 是一套基于 OAuth 2.0 协议的轻量级规范,提供通过 API 进行身份交互的框架 ...

  2. ASP.NET Web API编程——序列化与内容协商

    1 多媒体格式化器 多媒体类型又叫MIME类型,指示了数据的格式.在HTTP协议中多媒体类型描述了消息体的格式.一个多媒体类型包括两个字符串:类型和子类型. 例如: text/html.image/p ...

  3. JavaScript的基础学习

    由js和python想到的: 弱类型语言 js 中的数据在进行算数运算时,会自动转换类型强类型语言 变量的值的数据类型一旦确定,使用时不能改变 动态语言:编译时不知道数据类型,只有在执行时才知道数据类 ...

  4. zipline-benchmarks.py文件改写

    改写原因:在这个模块中的 get_benchmark_returns() 方法回去谷歌财经下载对应SPY(类似于上证指数)的数据,但是Google上下载的数据在最后写入Io操作的时候会报一个恶心的编码 ...

  5. ZOJ-1586 QS Network---最小生成树Prim

    题目链接: https://vjudge.net/problem/ZOJ-1586 题目大意: 首先给一个t,代表t个测试样例,再给一个n,表示有n个QS装置,接下来一行是n个QS装置的成本.接下来是 ...

  6. jacascript DOM节点——节点获取与选择器API

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! DOM 操作必须等待 HTML 加载完毕之后,才可以获取节点:有两种方法: 把 script 标签放到代码 ...

  7. [Nginx]-外部多端口映射Https443端口配置

    https服务器配置完成后,域名访问默认匹配至443端口,如果想同时通过https域名网址来请求多个对外服务,就需要在Nginx配置里来对请求进行规则判断,并匹配至相应的内部端口,这也是Nginx反向 ...

  8. MySQL高可用架构之MHA 原理与实践

    MHA简介 关于MHA MHA(Master HA)是一款开源的MySQL的高可用程序,它为MySQL主从复制架构提供了automating master failover 功能.MHA在监控到mas ...

  9. java设计模式之单例设计模式和多例设计模式

    单例设计模式:构造方法私有化,在类的内部定义static属性和方法,利用static方法来取得本类的实例化对象:无论外部产生多少个实例化对象,本质上只有一个实例化对象 饿汉式单例设计 class Si ...

  10. [C#]设计模式-简单工厂-创建型模式

    在设计模式当中有三大工厂,分别是 简单工厂.抽象工厂.工厂方法 这三种创建实例的设计模式,这里先从简单工厂将其,从名字就可以看出这是这三种工厂模式当中最为简单的一种实现. 简单工厂一般由以下几个对象组 ...