mongoDB常见的查询索引(三)
1. _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 }> |
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 }> |
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]})WriteResult({ "nInserted" : 1 }) |
4. 复合索引
|
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 }> |
5. 过期索引
|
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}){ "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 }> |
6.全文索引
|
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"}}){ "_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"}}){ "_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"}}){ "_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 }> |
7.地理位置索引
2d索引
|
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]}}) #会返回100个,理你最近的点{ "_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
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 }> |
|
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 }" }})> |
|
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字段,所以不会创建索引,所以查不到记录 > |
mongoDB常见的查询索引(三)的更多相关文章
- MongoDB 常见的查询索引
常见的查询索引 _id索引 _id 索引是绝大多数集合默认建立的索引.对于每一个插入的数据.MongoDB 会自己主动生成一条唯一的 _id 字段. 1 2 3 4 5 6 7 8 9 ...
- Mongodb 常见的查询语句及与 mysql 对比
db.users.find()select * from users db.users.find({"age" : 27})select * from users where ag ...
- Mongodb 笔记03 查询、索引
查询 1. MongoDB使用find来进行查询.find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件.空的查询会匹配集合的全部内容.要是不指定查询,默认是{}. 2. 可以 ...
- 【MongoDB详细使用教程】三、高级查询
目录 1.使用比较运算符查询 2.使用关键字查询 2.1.in/not in 关键字 2.2.size 关键字 2.3.exists 关键字 2.4.or 关键字 3.模糊查询 4.查询结果排序 5. ...
- 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 ...
- 搜索引擎学习(三)Lucene查询索引
一.查询理论 创建查询:构建一个包含了文档域和语汇单元的文档查询对象.(例:fileName:lucene) 查询过程:根据查询对象的条件,在索引中找出相应的term,然后根据term找到对应的文档i ...
- Mysql常见四种索引的使用
提到MySQL优化,索引优化是必不可少的.其中一种优化方式 --索引优化,添加合适的索引能够让项目的并发能力和抗压能力得到明显的提升. 我们知道项目性能的瓶颈主要是在"查(select)&q ...
- 【mongoDB中级篇②】索引与expain
索引的操作 数据库百分之八十的工作基本上都是查询,而索引能帮我们更快的查询到想要的数据.但是其降低了数据的写入速度,所以要权衡常用的查询字段,不必在太多字段上建立索引. 在mongoDB中默认是用bt ...
随机推荐
- 一个蒟蒻对FFT的理解(蒟蒻也能看懂的FFT)
建议同学们先自学一下"复数(虚数)"的性质.运算等知识,不然看这篇文章有很大概率看不懂. 前言 作为一个典型的蒟蒻,别人的博客都看不懂,只好自己写一篇了. 膜拜机房大佬 HY 一. ...
- Go 错误处理
Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } 我们可以在编码 ...
- 关于云Linux部署tomcat服务器(Maven的多模块war包)
博主的运行环境: 电脑系统: Linux mint 18 JDK版本: java version "1.8.0_171" Maven版本: Apache Maven 3.5.3 ...
- Android Studio精彩案例(六)《使用一个Demo涵盖补间动画所有知识》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 元旦假期里,闲的无事,看到美团加载数据的动画,就突想写个Demo把动画知识集成一下.后来想了想,还是直接用一个Demo来把所有动画知识 ...
- Rails做rspec测试时出现bcrypt错误的解决
在用rspec做测试的时候,出现了如下一句错误: You don't have bcrypt-ruby installed in your application. Please add it to ...
- Python强大的可变参数传递机制
今天模拟定义map函数.写着写着就发现Python可变长度参数的机制真是灵活而强大. 假设有一个元组t,包含n个成员: t=(arg1,...,argn) 而一个函数f恰好能接受n个参数: f(arg ...
- 全面剖析Redis Cluster原理和应用
全面剖析Redis Cluster原理和应用 1.Redis Cluster总览 1.1 设计原则和初衷 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最 ...
- Switch控件详解
Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...
- Android开发之手把手教你写ButterKnife框架(一)
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52662376 本文出自:[余志强的博客] 一.概述 JakeWhar ...
- Dynamics CRM2016 Web API之Retrieve Multiple
之前的博文只介绍了通过记录的primary key来查询单条记录或者单个属性值,本篇介绍多条记录的查询方法 var filter = "?$filter=name eq '123'" ...