1 / 15

MongoDB第二篇高级查询

MongoDB第二篇高级查询. 杭州联云汽车科技. 开篇导语. 面向文档的NoSQL数据库解决的主要问题不是高性能的并发读写,而是在保证存储海量数据的同时具有良好的查询性能。 今天主要介绍MongoDB常用的查询操作符和查询语法 了解后,也许你会蠢蠢欲动的想把MySQL切换到MongoDB上,革掉半条MySQL的命 !. MongoDB查询操作符. 先回顾上一篇几个命令 1、链接mongo客户端,命令:mongo 2、列出当前数据库,命令:show dbs 3、切换到使用的数据库,命令:use lianyun_tj 进入第二篇正题,查询操作符

donkor
Download Presentation

MongoDB第二篇高级查询

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MongoDB第二篇高级查询 杭州联云汽车科技

  2. 开篇导语 面向文档的NoSQL数据库解决的主要问题不是高性能的并发读写,而是在保证存储海量数据的同时具有良好的查询性能。 今天主要介绍MongoDB常用的查询操作符和查询语法 了解后,也许你会蠢蠢欲动的想把MySQL切换到MongoDB上,革掉半条MySQL的命 !

  3. MongoDB查询操作符 先回顾上一篇几个命令 1、链接mongo客户端,命令:mongo 2、列出当前数据库,命令:show dbs 3、切换到使用的数据库,命令:use lianyun_tj 进入第二篇正题,查询操作符 概念:操作符就是对数据进行操作的符合,表达了要对数据库执行的操作。数据库系统的每一条查询指令都有一个操作符,表示该指令应该执行什么性质的操作。 我们查询操作符,用到db.test.find()查询方法

  4. 条件操作符常用的:<,<=,>,>=//age > 18db.lianyun_tj.find({"age":{$gt:18}});//age < 18db.lianyun_tj.find("age":{$lt:18});//age >= 18db.lianyun_tj.find("age":{$gte:18});//age <= 18db.lianyun_tj.find("age":{$lte:18});//age >17 and age <19db.lianyun_tj.find("age":{$gt:17,$lt:19});

  5. 匹配所有$all操作符,这个和SQL的in类似区别:in只需要匹配括号内的某一个值,而$all必须满足括号内的所有值(这种查询条件只有在属性值是一个数组的情况下)db.lianyun_tj.find({age:{$all:[1,2,3]}});db.lianyun_tj.find(age:{$all:[1,3]})db.lianyun_tj.find(age:{$all:[1,3,4]})//查询不到,因为4在数组内没有匹配所有$all操作符,这个和SQL的in类似区别:in只需要匹配括号内的某一个值,而$all必须满足括号内的所有值(这种查询条件只有在属性值是一个数组的情况下)db.lianyun_tj.find({age:{$all:[1,2,3]}});db.lianyun_tj.find(age:{$all:[1,3]})db.lianyun_tj.find(age:{$all:[1,3,4]})//查询不到,因为4在数组内没有

  6. $exists判断字段是否存在用于判断某个字段是否存在//判断所有存在username字段的记录db.lianyun_tj.find(username:{$exists:true});//判断所有不存在username字段的记录db.lianyun_tj.find(username:{$exists:false});$exists判断字段是否存在用于判断某个字段是否存在//判断所有存在username字段的记录db.lianyun_tj.find(username:{$exists:true});//判断所有不存在username字段的记录db.lianyun_tj.find(username:{$exists:false}); • null值处理db.lianyun_tj.find()//查询没有age字段,或者age字段为空的数据db.lianyun_tj.find({age:nul});//这时会把没有age字段的记录也查询出来//我们通过exists限制下db.lianyun_tj.find({age:{"$in":[null],"$exists":true}});

  7. $ne不等于//age!=10db.lianyun_tj.find({age:{$ne:10}}); • $in包含此操作符与SQL标准语法的用途一样,即查询的数据在一个特定的取值范围内。列如 :我们查询age在9,10,18范围内的数据。db.lianyun_tj.find({age:{$in:[9,10,18]}}); • $size匹配数组元素个数此操作符用于统计数组中的元素个数。例如:{name:'lxm',age:18,favorite_number:[6,7,9]}它匹配以下查询db.lianyun_tj.find({favorite_number:{$size:3}});但不匹配以下查询db.lianyun_tj.find({favorite_number:{$size:2}});

  8. count查询记录条数此操作符用于统计记录的条数。db.lianyun_tj.find().count();//查找表lianyun_tj数量db.lianyun_tj.count();count查询记录条数此操作符用于统计记录的条数。db.lianyun_tj.find().count();//查找表lianyun_tj数量db.lianyun_tj.count(); • skip限制返回记录的起点此操作符用于过滤结果集中的某些行//从第2条记录开始,返回3条记录db.lianyun_tj.find().skip(2).limit(3); • sort排序此操作符用于将结果集排序//以age升序(asc)排序db.lianyun_tj.find().sort({age:1});//以age降序(desc)排序db.lianyun_tj.find().sort({age:-1});

  9. distinct去掉重复值在表中可能会包含重复值,distinct操作符用来过滤掉多余的重复记录,对于重复记录只保留一条。db.lianyun_tj.distinct("username");db.lianyun_tj.distinct("age");distinct去掉重复值在表中可能会包含重复值,distinct操作符用来过滤掉多余的重复记录,对于重复记录只保留一条。db.lianyun_tj.distinct("username");db.lianyun_tj.distinct("age"); • group分组统计group by 子语句主要用于对where中得到的结果进行分组。也就是说它在where子句之后执行,对经过where筛选后的结果按照某些列进行分组,之后进行相应的处理

  10. 传统SQLselect a,b,sum(c) csum from lianyun_tj where age=18 gropu by a,b • MongoDBdb.lianyun_tj( {key:{a:true,b:true}, cond:{age:18}, reduce:function(obj,prev) {prev.csum += obj.c;}, initial:{csum:0} }) 参数说明: key:要分组的列 cond:分组条件。类似SQL里的where条件内容 reduce:分组计算的方法 initial:分组计算的初始值,本例中指csum的值从0开始计算

  11. MongoDB查询语法结语 MongoDB最大的特点是,它支持的查询语言非常强大,其语法类似于面向对象的查询语言,不但可以实现关心型数据库查询的大部分功能,而且还支持对数据简历索引。 由于MongoDB可以支持非常复杂的数据结构,同时带有强大的数据查询功能,因此非常受欢迎,很多项目都考虑用MongoDB来替代MySQL等传统关系型数据库实现复杂的Web应用。 很多都是因为数据量实在太大,所以迁移到MongoDB上,从而数据查询的速度得到了非常明显的提升

  12. MongoDB数组内容的查询 • MongoDB本身支持数组类型的内嵌对象 • 数组内的查询方法跟普通查询方法没有区别,只不过要查询的是数组中的值,而不是单独的值//列如有这样一个记录//查询complay中有lianyun的记录db.lianyun_tj.find({complay:"lianyun"}); • 内嵌文档的查询如果某一列存储的不是一个单独的值,而是一条有多个列的记录,这条记录叫做“内嵌文档”。所以查询内嵌文档时,需要将条件细化到列上

  13. MongoDB内嵌文档的查询 //例如数据库中存在以下内嵌式文档数据,其中author列的值对应的就是内嵌文档。 //现在查询“author=neiqian2”的记录db.lianyun_tj.find({"author.username":"neiqian2"}); 注意:查询内嵌对象的属性时,记得要加上双引号。就是字段"author.username"

  14. MongoDB正则表达式匹配 & where查询 • 正则表达式匹配正则表达式就是用某种模式去匹配一类字符串的一个公式,它由一些普通的字符组成//查询不匹配 "username=arr*"开头的记录db.lianyun_tj.find({username:{$not:/^arr.*/}}); • $where查询$where查询允许在查询里执行JavaScript表达式。//查询age > 10的数据db.lianyun_tj.find({age:{$gt:10}});db.lianyun_tj.find({this.age > 10});//不推荐使用

More Related