工具推荐:Robomongo,可自行百度寻找下载源,个人比较推荐这个工具,相比较mongoVUE则更加灵活。

集合简单查询方法

mongodb语法:db.collection.find()  //collection就是集合的名称,这个可以自己进行创建。

对比sql语句:select * from collection;

查询集合中所有的文档,即关系型数据库中的查询表中的所有数据。

返回制定的键值

mongodb语法:db.collection.find({},{"userid":1})

对比sql语句:select userid from collection;

条件过滤

mongodb语法 : db.collection.find({"userid":495})

对比sql语句:select * from collectionwhere userid = 495;

  

查询全格式书写解释

  db.collection.find({},{})

第一个{}中,写入的都是相当于sql语句中where后的条件,多条件格式为{"key":value,"key2":"value2"}

第二个{}中,写入的都是相当于sql语句中跟在select后边的具体字段,格式为{"key":value,"key2":value}

      当value = 0时为不显示此字段值,当value !=0,即等于任何非0值时,则为显示此字段。

例:

mongodb查询:

db.error.find({"userid":1,"type":"debug"},{"userid":1,"type":1,"myssage":1})

sql查询:

select userid,type,message from error where userid=1 and type = "debug";

sort排序与limit返回固定条目数

mongodb查询:

db.collection.find({"userid":1,"type":"debug"},{"userid":1,"type":1,"myssage":1}).sort("time":-1).limit(10)

sql查询:

select userid,type,message from collection where userid=1 and type = "debug" order by time desc limit 10;

count返回结果集总数

mongodb查询:

db.collection.count()

sql查询:

select count(*) from collection;

查询操作符"$gt" -->大于操作符

mongodb查询:

db.collection.find({"userid":{"$gt":"494"}})

sql查询:

select * from collection where userid > 494;

 

查询操作符"$gte" -->大于等于

mongodb查询:

db.collection.find({"userid":{"$gte":"494"}})

sql查询:

select * from collection where userid >= 494;

 

查询操作符 "$lt"-->小于

mongodb查询:

db.collection.find({"userid":{"$lt":"494"}})

sql查询:

select * from collection where userid <494;

 

查询操作符"$lte"-->小于等于

mongodb查询:

db.collection.find({"userid":{"$lte":"494"}})

sql查询:

select * from collection where userid < =494;

 

查询操作符"$ne"-->不等于

mongodb查询:

db.collection.find({"userid":{"$ne":"494"}})

sql查询:

select * from collection where userid != 494;

 

查询操作符"null查询"-->空

mongodb查询:

db.collection.find({"userid":null})

sql查询:

select * from collection where userid is null;

 

查询操作符"$all"-->匹配所有

mongodb查询:

db.collection.find({"userid":{"$all":"494"}})

sql查询:

select * from collection where userid = 494;

    当文档类型为数组时,使用$all进行匹配,非数组类型使用时与单一匹配一样。


查询操作符"$size"-->用于数组查询,查询指定长度的数组

mongodb查询:

db.collection.find({"remark":{"$size":"3"}})

查询操作符"$in"--> 在范围内

mongodb查询:

db.collection.find({"userid":{"$in":["297","495"]}})

sql查询:

select * from collection where userid in (297,495);


查询操作符"$nin"-->不在范围内

mongodb查询:

db.collection.find({"userid":{"$nin":["297","495"]}})

sql查询:

select * from collection where userid not in (297,495);


查询操作符"$and"-->至少包含两个表达式,两个表达式都满足的文档返回

mongodb查询:

db.collection.find({"$and":[{"userid":"495"},{"type":"info"}]})

sql查询:

select * from collection where userid=495 and type='info';


查询操作符"$nor"-->至少包含两个表达式,两个表达式都不满足的文档返回

mongodb查询:

db.collection.find({"$nor":[{"userid":"495"},{"userid":"297"}]})

sql查询:

select * from collection where userid not in (297,495);


查询操作符"$not"-->找出不匹配表达式的文档,不能够单独使用,必须与其他表达式配合使用

mongodb查询:

db.collection.find({"userid":{"$not":{"$gt":"297"}}})

等同于:db.collection.find({"userid":{"$lte":"297"}}})

sql查询:

select * from collection where userid <=297;


查询操作符"$or"-->至少包含两个表达式,两个表达式至少满足一个的文档返回

mongodb查询:

db.collection.find({"$or":[{"userid":"495"},{"userid":"297"}]})

sql查询:

select * from collection where userid =297 or userid = 495;


查询操作符"$exists"-->查询文档中字段是否存在

mongodb查询:

db.collection.find({"$exists":"true"})


查询操作符"$mod"-->键值对变量参数取模,值等于另一个参数

mongodb查询:

db.collection.find({"userid":{"$mod":[10,7]}})

执行条件:userid字段值必须是数字,userid对10取模,值等于7的文档返回。

sql查询:

select * from collection where (user_id%10)=7


查询操作符"$regex"-->正则匹配

mongodb查询:

db.collection.find({"userid":/5$/})

sql查询:

select * from collection where userid like '%5';

  sql正则写法:      

     select * from collection where userid regexp ".5$";
 
   正则匹配userid的最后一位是5的,sql中只有使用regexp才可以使用复杂的正则表达式,使用Like的方式不可以进行复杂的正则匹配

查询操作符"$slice"-->控制返回的数组元素中的元素个数

mongodb查询:

db.collection.find({},{"remark":{"$slice":5})

remark数组键值,返回数组键值中的前5个元素

db.collection.find({},{"remark":{"$slice":[10,5]})

remark数组键值,返回数组键值中的第11到15个元素,偏移量为10,然后返回5个。

db.collection.find({},{"remark":{"$slice":-5})

remark数组键值,返回数组键值中的后5个元素

逐渐补充中......

Mongodb操作之查询(循序渐进对比SQL语句)的更多相关文章

  1. Mongodb操作之查询(循序渐进对比SQL语句)(转http://www.tuicool.com/articles/UzQj6rF)

    工具推荐:Robomongo,可自行百度寻找下载源,个人比较推荐这个工具,相比较mongoVUE则更加灵活. 集合简单查询方法 mongodb语法:db.collection.find()  //co ...

  2. 如何查找MySQL中查询慢的SQL语句

    如何查找MySQL中查询慢的SQL语句 更多 如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow ...

  3. 如何查找MySQL中查询慢的SQL语句(转载)

    转载自https://www.cnblogs.com/qmfsun/p/4844472.html 如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那 ...

  4. Mysql中 查询慢的 Sql语句的记录查找

    Mysql中 查询慢的 Sql语句的记录查找 慢查询日志 slow_query_log,是用来记录查询比较慢的sql语句,通过查询日志来查找哪条sql语句比较慢,这样可以对比较慢的sql可以进行优化. ...

  5. 数据库文件导入导出操作,以及赋予权限SQL语句

    1.导出数据库xxxx和tlog(经过测试,没有问题)# /data/mysql/bin/mysqldump -u root -ppassword qq9x | gzip > /home/xxx ...

  6. 提高数据库的查询速率及其sql语句的优化问题

    在一个千万级的数据库查寻中,如何提高查询效率? 1)数据库设计方面:  a.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. b.应尽量避免在 ...

  7. orm分组,聚合查询,执行原生sql语句

    from django.db.models import Avg from app01 import models annotate:(聚合查询) ret=models.Article.objects ...

  8. sql server 运维时CPU,内存,操作系统等信息查询(用sql语句)

    我们只要用到数据库,一般会遇到数据库运维方面的事情,需要我们寻找原因,有很多是关乎处理器(CPU).内存(Memory).磁盘(Disk)以及操作系统的,这时我们就需要查询他们的一些设置和内容,下面讲 ...

  9. 厚溥教育1718部数据库连接作业答案,分装一个操作数据库而无需写SQL语句的函数

    <?php header("Content-type:text/html;charset=utf8"); //PHP操作数据库的函数 function phpsql($dbc ...

随机推荐

  1. Spring Framework 下载链接_现在有空

    下载链接:http://repo.spring.io/libs-release-local/org/springframework/spring/ 点击打开链接 包括Spring的各个版本号: 3.2 ...

  2. head first c&lt;11&gt;在根据网络编程

    博文可以在一个大的网络通信实现,但是,一个人只能起到,我们能够给每个clientfork()子进程,实现诸多的服务. 方法: client连到server以后,server启动一个新创建的套接字对话. ...

  3. sqlserver缓存程序-只能使用一次清除缓存计划

    plan cache非常大.将仅仅使用一次的缓存计划清除,而不用清除整个cache. declare @sid varbinary(64) declare cur01 cursor for selec ...

  4. 基于Gsoap 的ONVIF C++ 库

    https://github.com/xsmart/onvifcpplib 该库支持ProfileS 和ProfileG,目前正在开发哪些,现拥有支持Event 下面是一个client样本 int _ ...

  5. 淘宝异构数据源数据交换工具 DataX

    淘宝异构数据源数据交换工具 DataX 阅读目录 DataX是什么? DataX用来解决什么? DataX特点? DataX结构模式(框架+插件) DataX在淘宝的运用 DataX是什么? Data ...

  6. SQL Server 优化存储过程的七种方法

    原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...

  7. Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 3

    DDL Setup Steps SQL> grant execute on utl_file to ggs; Grant succeeded. Create GLOBALS file [orac ...

  8. HDU——B-number(数字DP)

    标题效果: 要了解1至n如何号码之间有许多含有13,并13可分 记忆化搜索: dp[pos][pre][mod][statu],pos位数,pre前一位,mod余数,statu状态 有2个状态:含13 ...

  9. Android Studio常见报错及处理办法

    在Android Studio上点了update,系统自动升级,自动重启Android Studio后,以前的项目Gradle正常编译: Unable to start the daemon proc ...

  10. PC结束 Spark 二次开发 收到自己主动,并允许好友请求

    本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...