MongoDB 常见的查询索引
常见的查询索引
_id索引
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
> db.jerome_2.collection.insert({x:2})WriteResult({ "nInserted" : 1 })> db.jerome_2.collection.getIndexes()[ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "jerome.jerome_2.collection" }]> db.jerome_2.collection.findOne(){ "_id" : ObjectId("557004f1f2824fa15224e20b"), "x" : 2 }> |
单键索引
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
> db.jerome_2.collection.ensureIndex({x:1}) # 创建单键索引{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1}> db.jerome_2.collection.getIndexes()[ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "jerome.jerome_2.collection" }, { "v" : 1, "key" : { "x" : 1 }, "name" : "x_1", "ns" : "jerome.jerome_2.collection" }]> db.jerome_2.collection.find({x:1}) #使用创建的索引查询{ "_id" : ObjectId("557005a5f2824fa15224e20c"), "x" : 1, "y" : 2, "z" : 3 }> |
多键索引
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
> db.jerome_2.collection.getIndexes()[ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "jerome.jerome_2.collection" }, { "v" : 1, "key" : { "x" : 1 }, "name" : "x_1", "ns" : "jerome.jerome_2.collection" }]> db.jerome_2.collection.find(){ "_id" : ObjectId("557004f1f2824fa15224e20b"), "x" : 2 }{ "_id" : ObjectId("557005a5f2824fa15224e20c"), "x" : 1, "y" : 2, "z" : 3 }> db.jeroem_2.collection.insert({x:[1,2,3,4,5]}) #插入一条数组数据,对于这条数据来讲。mongodb为其创建了一个多键索引WriteResult({ "nInserted" : 1 }) |
复合索引
|
1
2
3
4
5
6
7
8
9
10
|
> db.jerome_2.collection.ensureIndex({x:1,y:1}) #创建{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1}> db.jerome_2.collection.find({x:1,y:2}) #使用{ "_id" : ObjectId("557005a5f2824fa15224e20c"), "x" : 1, "y" : 2, "z" : 3 }> |
过期索引
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
> db.jerome_2.collection.ensureIndex({time:1},{expireAfterSeconds:30}) #创建过期索引,过期时间30秒{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 3, "numIndexesAfter" : 4, "ok" : 1}> db.jerome_2.collection.insert({time:new Date()}) #插入数据測试WriteResult({ "nInserted" : 1 })> db.jerome_2.collection.find(){ "_id" : ObjectId("557004f1f2824fa15224e20b"), "x" : 2 }{ "_id" : ObjectId("557005a5f2824fa15224e20c"), "x" : 1, "y" : 2, "z" : 3 }{ "_id" : ObjectId("55700b17f2824fa15224e20e"), "time" : ISODate("2015-06-04T08:23:51.531Z") }> db.jerome_2.collection.find(){ "_id" : ObjectId("557004f1f2824fa15224e20b"), "x" : 2 }{ "_id" : ObjectId("557005a5f2824fa15224e20c"), "x" : 1, "y" : 2, "z" : 3 }{ "_id" : ObjectId("55700b17f2824fa15224e20e"), "time" : ISODate("2015-06-04T08:23:51.531Z") }> db.jerome_2.collection.find() #时间过了就找不到了{ "_id" : ObjectId("557004f1f2824fa15224e20b"), "x" : 2 }{ "_id" : ObjectId("557005a5f2824fa15224e20c"), "x" : 1, "y" : 2, "z" : 3 }> |
(必须是ISODate或者ISODate数组,不能使用时间戳。否则不能被自己主动删除)
全文索引
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
> db.jerome_2.ensureIndex({"article":"text"}) #创建全文可搜索索引{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1}> db.jerome_2.insert({"article":"aa bb cc dd ee"}) #插入測试数据WriteResult({ "nInserted" : 1 })> db.jerome_2.insert({"article":"aa bb rr gg zz"})WriteResult({ "nInserted" : 1 })> db.jerome_2.insert({"article":"aa bb"})WriteResult({ "nInserted" : 1 })> db.jerome_2.insert({"article":"aa bb cc zz ff ww"})WriteResult({ "nInserted" : 1 })> db.jerome_2.find({$text:{$search:"aa"}}) #查找{ "_id" : ObjectId("5572904271c0bbd90f4ce0e2"), "article" : "aa bb rr gg zz" }{ "_id" : ObjectId("5572903371c0bbd90f4ce0e1"), "article" : "aa bb cc dd ee" }{ "_id" : ObjectId("5572905671c0bbd90f4ce0e4"), "article" : "aa bb cc zz ff ww" }{ "_id" : ObjectId("5572904771c0bbd90f4ce0e3"), "article" : "aa bb" }> db.jerome_2.find({$text:{$search:"ff"}}){ "_id" : ObjectId("5572905671c0bbd90f4ce0e4"), "article" : "aa bb cc zz ff ww" }> db.jerome_2.find({$text:{$search:"aa bb cc"}}) #空格表示或{ "_id" : ObjectId("5572904271c0bbd90f4ce0e2"), "article" : "aa bb rr gg zz" }{ "_id" : ObjectId("5572903371c0bbd90f4ce0e1"), "article" : "aa bb cc dd ee" }{ "_id" : ObjectId("5572905671c0bbd90f4ce0e4"), "article" : "aa bb cc zz ff ww" }{ "_id" : ObjectId("5572904771c0bbd90f4ce0e3"), "article" : "aa bb" }> db.jerome_2.find({$text:{$search:"aa bb -cc"}}) #-cc 表示不包括cc{ "_id" : ObjectId("5572904271c0bbd90f4ce0e2"), "article" : "aa bb rr gg zz" }{ "_id" : ObjectId("5572904771c0bbd90f4ce0e3"), "article" : "aa bb" }> db.jerome_2.find({$text:{$search:"\"aa\" \"bb\" \"cc\""}}) #加引號,表示与{ "_id" : ObjectId("5572903371c0bbd90f4ce0e1"), "article" : "aa bb cc dd ee" }{ "_id" : ObjectId("5572905671c0bbd90f4ce0e4"), "article" : "aa bb cc zz ff ww" }> |
|
1
2
3
4
5
6
7
8
9
10
11
|
> db.jerome_2.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}) #score越高,类似度越高。{ "_id" : ObjectId("5572904271c0bbd90f4ce0e2"), "article" : "aa bb rr gg zz", "score" : 1.2 }{ "_id" : ObjectId("5572903371c0bbd90f4ce0e1"), "article" : "aa bb cc dd ee", "score" : 1.2 }{ "_id" : ObjectId("5572905671c0bbd90f4ce0e4"), "article" : "aa bb cc zz ff ww", "score" : 1.1666666666666667 }{ "_id" : ObjectId("5572904771c0bbd90f4ce0e3"), "article" : "aa bb", "score" : 1.5 }> db.jerome_2.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}}) #依据score进行排序{ "_id" : ObjectId("5572904771c0bbd90f4ce0e3"), "article" : "aa bb", "score" : 1.5 }{ "_id" : ObjectId("5572903371c0bbd90f4ce0e1"), "article" : "aa bb cc dd ee", "score" : 1.2 }{ "_id" : ObjectId("5572904271c0bbd90f4ce0e2"), "article" : "aa bb rr gg zz", "score" : 1.2 }{ "_id" : ObjectId("5572905671c0bbd90f4ce0e4"), "article" : "aa bb cc zz ff ww", "score" : 1.1666666666666667 }> |
地理位置索引
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
> db.location.ensureIndex({"w":"2d"}) #创建2d索引> db.location.insert({w:[1,1]}) #插入測试数据。经纬度[经度,纬度]。
WriteResult({ "nInserted" : 1 })> db.location.insert({w:[1,2]})WriteResult({ "nInserted" : 1 })> db.location.insert({w:[2,3]})WriteResult({ "nInserted" : 1 })> db.location.insert({w:[100,80]})WriteResult({ "nInserted" : 1 })> db.location.find({w:{$near:[1,1]}}) #查询距离某个点近期的点{ "_id" : ObjectId("5572a961aba41684d6e8826c"), "w" : [ 1, 1 ] }{ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] }{ "_id" : ObjectId("5572a970aba41684d6e8826e"), "w" : [ 2, 3 ] }{ "_id" : ObjectId("5572a97aaba41684d6e8826f"), "w" : [ 100, 80 ] }> db.location.find({w:{$near:[1,1],$maxDistance:2}}) #能够使用maxDistance限制(near不能使用minDistance){ "_id" : ObjectId("5572a961aba41684d6e8826c"), "w" : [ 1, 1 ] }{ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
> db.location.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}}) #矩形{ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] }{ "_id" : ObjectId("5572a970aba41684d6e8826e"), "w" : [ 2, 3 ] }{ "_id" : ObjectId("5572a961aba41684d6e8826c"), "w" : [ 1, 1 ] }> db.location.find({w:{$geoWithin:{$box:[[1,2],[2,3]]}}}){ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] }{ "_id" : ObjectId("5572a970aba41684d6e8826e"), "w" : [ 2, 3 ] }> db.location.find({w:{$geoWithin:{$center:[[0,0],100]}}}) #圆形。100是半径{ "_id" : ObjectId("5572a961aba41684d6e8826c"), "w" : [ 1, 1 ] }{ "_id" : ObjectId("5572a970aba41684d6e8826e"), "w" : [ 2, 3 ] }{ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] }> db.location.find({w:{$geoWithin:{$center:[[0,0],1000]}}}){ "_id" : ObjectId("5572a961aba41684d6e8826c"), "w" : [ 1, 1 ] }{ "_id" : ObjectId("5572a97aaba41684d6e8826f"), "w" : [ 100, 80 ] }{ "_id" : ObjectId("5572a970aba41684d6e8826e"), "w" : [ 2, 3 ] }{ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] }> db.location.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}}) #多边形查询(各个点围成的多边形的范围){ "_id" : ObjectId("5572a970aba41684d6e8826e"), "w" : [ 2, 3 ] }{ "_id" : ObjectId("5572a961aba41684d6e8826c"), "w" : [ 1, 1 ] }{ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] }> db.location.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1000],[1001,0]]}}}){ "_id" : ObjectId("5572a970aba41684d6e8826e"), "w" : [ 2, 3 ] }{ "_id" : ObjectId("5572a97aaba41684d6e8826f"), "w" : [ 100, 80 ] }{ "_id" : ObjectId("5572a961aba41684d6e8826c"), "w" : [ 1, 1 ] }{ "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
> db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1}){ "results" : [ { "dis" : 0, "obj" : { "_id" : ObjectId("5572a965aba41684d6e8826d"), "w" : [ 1, 2 ] } } ], "stats" : { "nscanned" : NumberLong(1), "objectsLoaded" : NumberLong(1), "avgDistance" : 0, "maxDistance" : 0, "time" : 2 }, "ok" : 1}> |
创建索引比較重要属性介绍
1. 名字,name指定:db.collection.ensureIndex({},{name:""})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
> db.jerome_2.ensureIndex({x:1})> db.jerome_2.ensureIndex({y:-1})> db.jerome_2.getIndexes()[ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "jerome.jerome_2" }, { "v" : 1, "key" : { "x" : 1 }, "name" : "x_1", "ns" : "jerome.jerome_2" }, { "v" : 1, "key" : { "y" : -1 }, "name" : "y_-1", "ns" : "jerome.jerome_2" } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
> db.jerome_2.ensureIndex({x:1,y:-1})> db.jerome_2.ensureIndex({x:1,y:-1,z:1})> db.jerome_2.getIndexes()[ { "v" : 1, "key" : { "x" : 1, "y" : -1 }, "name" : "x_1_y_-1", "ns" : "jerome.jerome_2" }, { "v" : 1, "key" : { "x" : 1, "y" : -1, "z" : 1 }, "name" : "x_1_y_-1_z_1", "ns" : "jerome.jerome_2" }] |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
> db.jerome_2.ensureIndex({x:1,y:1,z:1,m:1},{name:"normal_index"})> db.jerome_2.getIndexes()[ { "v" : 1, "key" : { "x" : 1, "y" : 1, "z" : 1, "m" : 1 }, "name" : "normal_index", "ns" : "jerome.jerome_2" }]> db.jerome_2.dropIndex("normal_index"){ "nIndexesWas" : 7, "ok" : 1 }> |
2. 唯一性。unique 指定:db.collection.ensureIndex({},{unique:true/false})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
> db.jerome.ensureIndex({m:1,n:1},{unique:true}){ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1}> db.jerome.insert({m:1,n:2})WriteResult({ "nInserted" : 1 })> db.jerome.insert({m:1,n:2})WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: jerome.jerome.$m_1_n_1 dup key: { : 1.0, : 2.0 }" }})> |
3. 稀疏性,sparse 指定:db.collection.ensureIndex({},{sparse:true/false})
由于 MongoDB 没有固定的格式,插入的时候可能插入不存在的字段。比方x:1,MongoDB 会为这条不存在的字段创建索引。假设不希望发现这种事情能够指定稀疏索引为 true,就不会为不存在的字段创建索引了。
能够降低磁盘占用和增大插入速度。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
> db.jerome.insert({"m":1})WriteResult({ "nInserted" : 1 })> db.jerome.insert({"n":1})WriteResult({ "nInserted" : 1 })> db.jerome.find({m:{$exists:true}}) #exists查找数据集合中一个字段存在或者不存在的记录{ "_id" : ObjectId("55729ec1aba41684d6e8826a"), "m" : 1 }{ "_id" : ObjectId("55729d5caba41684d6e88268"), "m" : 1, "n" : 2 }> db.jerome.ensureIndex({m:1},{sparse:true}) #创建稀疏索引{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1}> db.jerome.find({m:{$exists:false}}) #MongoDB内部问题。所以找得到,通过以下强制指定索引{ "_id" : ObjectId("55729ec7aba41684d6e8826b"), "n" : 1 }> db.jerome.find({m:{$exists:false}}).hint("m_1") #以下这条记录。不存在m字段,所以不会创建索引。所以查不到记录 > |
4. 是否定时删除
MongoDB 常见的查询索引的更多相关文章
- mongoDB常见的查询索引(三)
1. _id索引 _id索引是绝大多数集合默认建立的索引 对于每个插入的数据,MongoDB会自动生成一条唯一的_id字段. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Mongodb 常见的查询语句及与 mysql 对比
db.users.find()select * from users db.users.find({"age" : 27})select * from users where ag ...
- spring Mongodb查询索引报错 java.lang.NumberFormatException: empty String
最近事情比较多,本篇文章算是把遇到的问题杂糅到一起了. 背景:笔者最近在写一个mongo查询小程序,由于建立索引时字段名用大写,而查询的时候用小写. 代码如下: db.getCollection(&q ...
- java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作
官方api教程:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started ...
- Mongodb 笔记03 查询、索引
查询 1. MongoDB使用find来进行查询.find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件.空的查询会匹配集合的全部内容.要是不指定查询,默认是{}. 2. 可以 ...
- MongoDB优化,建立索引实例及索引机制原理讲解
MongoDB优化,建立索引实例及索引机制原理讲解 为什么需要索引? 当你抱怨MongoDB集合查询效率低的时候,可能你就需要考虑使用索引了,为了方便后续介绍,先科普下MongoDB里的索引机制(同样 ...
- mongodb 高级聚合查询
mongodb高级聚合查询 在工作中会经常遇到一些mongodb的聚合操作,特此总结下.mongo存储的可以是复杂类型,比如数组.对象等mysql不善于处理的文档型结构,并且聚合的操作也比mysq ...
- Mysql常见四种索引的使用
提到MySQL优化,索引优化是必不可少的.其中一种优化方式 --索引优化,添加合适的索引能够让项目的并发能力和抗压能力得到明显的提升. 我们知道项目性能的瓶颈主要是在"查(select)&q ...
- 【mongoDB中级篇②】索引与expain
索引的操作 数据库百分之八十的工作基本上都是查询,而索引能帮我们更快的查询到想要的数据.但是其降低了数据的写入速度,所以要权衡常用的查询字段,不必在太多字段上建立索引. 在mongoDB中默认是用bt ...
随机推荐
- 本地yum仓库的搭建
. 1.直接断开网络,模拟生产内网环境 2.将原先的网络yum仓库全部移动到 backup目录下 3.创建本地yum仓库 local_yum.repo vi /etc/yum.repos.d/loc ...
- 使用bottle进行web开发(3):静态文件的获取
静态文件(比如css啊,需要下载的各位文件等),需要通过static_file来操作,首先记得要在import中导入 @route('/static/<filename>') def se ...
- Simditor 富文本编辑器
Simditor 是团队协作工具 Tower 使用的富文本编辑器. 相比传统的编辑器它的特点是: 功能精简,加载快速 输出格式化的标准 HTML 每一个功能都有非常优秀的使用体验 兼容的浏览器:IE1 ...
- hdu 1546(dijkstra)
Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- ros navigation stack---move_base
大部分内容参考自: ros_by_example_hydro_volume_1.pdf 主要是讲如何让先锋机器人在空白地图上运动 上面图是navigation框架图,可以看到move_base处在核心 ...
- MVC5 WebAPI 跨域处理
问题描述: 在使用ASP.NET的MVC5进行WebAPI开发的时候,在跨域的情况下会报跨域的错, No 'Access-Control-Allow-Origin' header is present ...
- Codeforces Round #448 (Div. 2) A. Pizza Separation【前缀和/枚举/将圆(披萨)分为连续的两块使其差最小】
A. Pizza Separation time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Python与数据库[1] -> 数据库接口/DB-API[1] -> MySQL 适配器
MySQL适配器 / MySQL Adapter MySQL是一种关系型数据库,下面主要介绍利用如何利用Python的MySQL适配器来对MySQL进行操作,其余内容可参考文末相关阅读. 1 MySQ ...
- Bfs【p2385】 青铜莲花池
题目描述--->p2385 青铜莲花池 分析 很明显了,题目告诉我们有八个方向,当然优先考虑bfs! 很简单的bfs,重点在于考虑清楚8个方向. 自己刚开始打错了 emmm 给大家上一个图.↓ ...
- Unity3D AssetBundles 动态加载游戏资源
AssetBundles are files which you can export from Unity to contain assets of your choice. These files ...