Mysql与MongoDB查询互转

mongo查询严格要求数据格式!

1、只想查出某些数据,不想全部数据都查出来

mysql:select name from user;

mongo:

db.user.find(

{},

{

_id :0,

name :1}

)

说明:user是集合的名称,find里面两个{},第一个留空是想所有数据都查出来,加条件的话类型mysql的where,第二个{}表示的意思类似mysql后面的select部分,0代表不显示,1代表显示。

2、分页查询

mysql:select * from user limit 0,10;

mongo:

db.user.find({}).skip(0).limit(10)

说明:mongo的skip和limit与mysql同理,mysql的limit第一个参数是跳过的数据量与mongo的skip类似,比如第三页的数据是从20开始的,mysql:limit 20,10,即:limit (page-1)*size,size

3、条件查询

mysql:select name from user where id = 1;

mongo:

db.user.find(

{ id :1},

{ name :1}

)

说明:由于有严格要求数据格式,若存到mongo的id是字符串格式的话,查询的条件得加上双引号""

4、范围查询

MySQL

MongoDB

remark

>

$gt

大于

<

$lt

小于

>=

$gte

大于等于

<=

$lte

小于等于

!=

$ne

不等于

mysql:select name from user where id > 1 and id < 10;

mongo:

db.user.find(

{

id : {

$gt :1,

$lt :10}

},

{ name :1}

)

说明:mysql的between其实就是>=和<=,字符串的话用范围查询好像会有问题,慎用!

5、in查询

mysql:select name from user where id in (1,2);

mongo:

db.user.find(

{

id : {

$in : [1, 2]}

},

{ name :1}

)

说明:not in查询就把$in换成$nin

6、条件统计count

mysql:select count(*) from user where id > 1;

mongo:

db.user.find(

{

id : {

$gt:1}

}

).count()

7、all查询

mongo可以将数组存储起来,若想查询某个字段(是个数组)同时包含值a和b

db.user.find(

{

detail: {

$all : ["7", "8"]}

}

)

说明:这个查询的结果集,detail字段同时包含字符串7和字符串8

8、exists查询

比如我想找出所有包含字段name_real的结果集

db.user.find(

{

name_real : {

$exists: true

}

}

)

说明:上面查询的结果中,所有数据肯定都包含有name_real字段;改成false的话就变成不包含

9、is null查询

mysql:select * from user where age is null;

mongo:

db.user.find(

{ age :null}

)

但是这样会有问题,这个查询会把那些没有age字段的结果也查出来,结合exists优化下

db.user.find(

{

age: {

$in : [null],

$exists: true

}

}

)

查询is not null

db.user.find(

{

age: {

$ne :null,

$exists: true

}

}

)

10、取模运算

mongo提供取模运算,比如存储了一些数据,我想查出那些取模后等于某个值的数据可以使用$mod

比如下例查询年龄是10的倍数的用户

mysql:select * from user where age % 10 = 0;

mongo:

db.user.find(

{

age:{

$mod :[10 , 0]}

}

)

11、查询数据元素个数

由于mongo可以存储数组,如果想查询数组中只有两个元素的记录时,可以使用$size

比如下例查询有三个兴趣爱好的用户

db.user.find(

{

favorite: {

$size:3}

}

)

12、正则匹配查询

如果想用正则匹配查询,可以使用$regex

比如下例匹配年龄是10以下的用户

db.user.find(

{

age: {

$regex:/^([1-9])$/}

}

)

13、只取一部分数据

类似mysql的limit,mongo也可以只取一部分数据

mysql:select * from user limit 10;

mongo:

db.user.find().limit(10)

14、排序

MySQL

MongoDB

说明

asc

1

升序

desc

-1

降序

mysql:select * from user order by age asc;

mongo:

db.user.find().sort(

{age:1}

)

说明:mongo字符串类型的也可以排序

15、求和

直接累加求和某一项

比如下例对年龄进行求和

mysql:select sum(age) as total from user;

mongo:

db.user.aggregate([{

$group:

{

_id: null,

total: {

$sum: "$age"

}

}

}])

分组求和

下例为按类型分组并求和

mysql:select type,sum(age) as total from user group bytype;

mongo:

db.user.aggregate([{

$group:

{

_id: "$type",

total: {

$sum: "$age"

}

}

}])

多条件分组求和

下例为按多个条件进行分组并求和

mysql:select type,sex,sum(age) as total from user group bytype,sex;

mongo:

db.user.aggregate([{

$group: {

_id:{

type: "$type",

sex: "$sex"

},

total: {

$sum: "$age"

}

}

}])

16、分组后having

下例为按条件分组并筛选出求和后大于100的数据

mysql:select type, sum(age) as total from user group by type having total > 100;

mongo:

db.user.aggregate([{

$group:

{

_id: "$type",

total: {

$sum: "$age"

}

}

},

{

$match: {

total: {

$gt: 100

}

}

}])

17、条件分组

类似mysql的where+group by进行查询

下例为查找出2020-01-01(timestamp:1577808000)后注册的用户,并按类型分组求和

mysql:select type,sum(age) as total from user where created > 1577808000 group bytype;

mongo:

db.user.aggregate([{

$match: {

created: { $gt: 1577808000 }

}

},

{

$group: {

_id: "$type",

total: { $sum: "$age" }

}

}])

条件分组并having筛选

下例为查找出2020-01-01(timestamp:1577808000)后注册的用户,并按类型分组,同时筛选出大于100的数据

mysql:select type,sum(age) as total from user where created > 1577808000 group by type having total > 100;

mongo:

db.user.aggregate([{

$match: {

created: { $gt: 1577808000 }

}

},

{

$group: {

_id: "$type",

total: { $sum: "$age" }

}

},

{

$match: {

total: { $gt: 100 }

}

}])

18、unwind

加入你的mongo的每一条记录有一个字段,存的是一个数组,数组里面是对象,类似这样,article字段含有

[{ "uid" : 1, "title" : "XXX", "content" : "XXX", "views" : 10 },

{ "uid" : 2, "title" : "XXX", "content" : "XXX", "views" : 11 },

{ "uid" : 3, "title" : "XXX", "content" : "XXX", "views" : 12 }]

使用unwind可以使上面原本一条记录进行展开,分为三条数据进行展示,有点像mysql的join查询,只不过mysql得分开两个表存

mysql:select * from user as u left join article as a on (u.id=a.uid);

mongo:

db.user.aggregate([{ $unwind: "$article" }])

unwind后求和

mysql:select sum(views) as total from user as u left join article as a on (u.id=a.uid)) asdata

mongo:

db.user.aggregate([{ $unwind: "$article" },

{

$group: {

_id: null,

total: { $sum: "$article.views" }

}

}])

19、分组后统计总共有多少组

下例分按类型分组,并统计总数

mysql:select count(*) from (select type from user group bytype);

mongo:

db.user.aggregate([{

$group: {

_id: "$type"

}

},

{

$group:

{

_id : null,

count: { $sum: 1 }

}

}])

20、aggregate类型linux的grep指令,像管道处理一样,一级接一级,比如:筛选、分组、过滤等,最后返回结果

db.user.aggregate([{ $match: { sex: "boy" } },

{ $group: { _id: "$type", total: { $sum: "$age" } } }])

未完。。。

跨mysql和mongodb查询工具_MySQL与MongoDB查询互转相关推荐

  1. mysql跨库查询 索引_MySQL中跨库查询怎么搞?

    导读 在MySQL中跨库查询主要分为两种情况,一种是同服务的跨库查询;另一种是不同服务的跨库查询;它们进行跨库查询是不同的,下面就具体介绍这两种跨库查询. 在MySQL中跨库查询主要分为两种情况,一种 ...

  2. mysql多表连接 索引_MySQL多表查询之外键、表连接、子查询、索引

    一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, ...

  3. mysql mongodb 集群_MySQL与MongoDB

    MySQL与MongoDB 声明: MySQL与MongoDB都是开源的常用数据库, 但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库. ...

  4. mysql的基本的查询语句_Mysql的基本查询语句

    聚集函数 mysql有5个聚集函数,分别是AVG,MAX,MIN,SUM,COUNT. 分组 分组的使用group by作为关键字,having作为条件关键字. having和where的区别:1.w ...

  5. mysql 并发 压测工具_MySQL压测工具mysqlslap的介绍与使用

    一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和 ...

  6. mysql 数据库日志管理工具_mysql mysqlbinlog日志管理工具使用教程

    一.概述 由于服务器生成的二进制日志文件以二进制格式保存,所以如果要想检查这些文件的文本格式,就会用到mysqlbinlog日志管理工具. mysqlbinlog的语法如下: mysqlbinlog ...

  7. mysql慢日志分析工具_mysql慢查日志分析工具 percona-toolkit

    备忘自: http://blog.csdn.net/seteor/article/details/24017913 1. 工具简介 pt-query-digest是用于分析mysql慢查询的一个工具, ...

  8. mysql 单表子查询语句_MySQL基本SQL查询语句:多表查询和子查询示例

    一.简单查询:基本语法: 代码如下 SELECT * FROM tb_name; 查询全部 代码如下 SELECT field1,field2 FROM tb_name; 投影 代码如下 SELECT ...

  9. Mysql 修改 复杂的汇总_MySQL数据分析:复杂查询

    本篇文章内容为;MySQL的复杂查询,针对工作中常见的sql操作,提出自己的意见. 主题为:视图.子查询(包括标量子查询.关联子查询) 首先,我们依旧先进入提问环节:如果我们需要经常性的某列数据进行汇 ...

最新文章

  1. js数组如何按照固定的下标去重_js数组去重的三种常用方法总结
  2. ACL 2021 | 腾讯AI Lab、港中文杰出论文:用单语记忆实现高性能NMT
  3. ajax提交form表单数据_[基础编程学习] [PHP7数组详解]:第2章 (1)从表单提交数据说起...
  4. 【BZOJ2625】[Neerc2009]Inspection 最小流
  5. tf.truncated_normal的用法
  6. WTM框架使用技巧之:Layui版本嫁接Vue+ElementUI
  7. 李迟2021年10月知识总结
  8. 遗传算法求解装箱问题c语言,装箱或背包问题? (或者遗传算法解决)
  9. 如何配置RadASM
  10. asp中 打开网页时出现“操作必须使用一个可更新的查询”原因及解决办法
  11. MOS管的工作原理以及设计理念
  12. 剑指offe--构建乘积数组
  13. DirtyWordsFilter(脏字过滤)
  14. 【Vue】Vue中传值的几种方法,案例代码解析
  15. riscv-amo原子指令
  16. What is hosts?
  17. excel单元格下拉选项怎么设置_Excel单元格内容这样设置才不能被修改
  18. 学生信息管理系统(C语言版本+源码)
  19. led点阵---显示数字或汉字(内含代码+注释+图片)
  20. Preparation and Practice

热门文章

  1. ossim系统下nagios监控机器可用性用rrd图形显示
  2. CSS学习笔记(更新中...)
  3. 信息学奥赛一本通 1102:与指定数字相同的数的个数 | OpenJudge NOI 1.6 01
  4. 信息学奥赛一本通(1248:Dungeon Master)
  5. 信息学奥赛一本通(1007:计算(a+b)×c的值)
  6. 理论基础 —— 排序 —— 直接插入排序
  7. 信息学奥赛C++语言: 商品排序
  8. 信息学奥赛C++语言:单词转换
  9. 信息学奥赛C++语言:格莱尔的香蕉
  10. android中viewpager+fragment,ViewPager和Fragment一篇就够了