find文档

1.find简介

使用find查询集合中符合条件的子集合

 db.test.blog.find();  

类似于sql查询

 select * from test.blog 

上面的查询是返回多有多有集合,并且是所有键。有时我们也会指定返回部分键,这样方式可以减少IO

 > db.test.blog.find({},{"age":1,"name":1});
{ "_id" : 1, "age" : 1, "name" : "joe" }
{ "_id" : ObjectId("533a1c27b653a97435a02030") }
{ "_id" : "2" }

类似sql查询

 select age , name from test.blog  

使用带条件的查询举例:

 > db.test.blog.find({"age":1});
{ "_id" : 1, "age" : 1, "like" : [ "eat", "abc" ], "name" : "joe" }

类似sql查询

 select * from test.blog where age = 1  

2.查询条件

比较操作符:

命令 描述
$lt <
$lte <=
$gt >
$gte >=
 > db.student.find({age:{$lte:12,$gte:10}}); 

类似sql

 select * from student where age >= 10 and age <= 12 

$ne:不等,能用于多有类型的数据

 > db.student.find({age:{$ne:10}});  

类似sql

 select * from student where age != 10

3.OR查询

命令 描述
$in 查询一个键的多个值
$or 用来完成多个键值的任意给定值

$in用法代码

 > db.student.find({age:{$in:[10,13]}});
{ "_id" : 0, "name" : "aa0", "age" : 10 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }

类比sql

 select * from student where age in (10,13);

$or代码

 > db.student.find({"$or":[{"age":{$in:[10,13]}},{"name":"aa7"}]});
{ "_id" : 0, "name" : "aa0", "age" : 10 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }

类比sql

 select * from student where age in (10,13) or name = 'aa0'  

4.$not

$not是原条件句,可以用在任何其他条件之上

 > db.student.find({"age":{$not:{"$mod":[5,1]}}});
{ "_id" : 0, "name" : "aa0", "age" : 10 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }
{ "_id" : 9, "name" : "aa9", "age" : 14 }

$mod:将查询的值除以第一个参数“5”,如果余数等于第二个参数“1”那么返回该值

5.null

null可以匹配自身(key对应的值为null),还可以匹配“不存在的”(文档中不存在这个key)。

 > db.student.find({"like":null});
{ "_id" : 1, "name" : "aa1", "age" : 11 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 6, "name" : "aa6", "age" : 11 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }
{ "_id" : 9, "name" : "aa9", "age" : 14 }
{ "_id" : 0, "age" : 10, "like" : null, "name" : "aa0" } > db.student.find({"like":null,"like":{$exists:true}});
{ "_id" : 0, "age" : 10, "like" : null, "name" : "aa0" }

说明:$exists:判断键是否存在

6.正则表达式

 > db.student.find({"name":/aa/});
{ "_id" : 1, "name" : "aa1", "age" : 11 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 6, "name" : "aa6", "age" : 11 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }
{ "_id" : 9, "name" : "aa9", "age" : 14 }
{ "_id" : 0, "age" : 10, "like" : null, "name" : "aa0" }
> db.student.find({"name":/aa1/});
{ "_id" : 1, "name" : "aa1", "age" : 11 }

使用/reg/来表示正则

7.查询数组

命令 描述
$all 匹配数组中的多个元素
$size 匹配数组的长度
$slice 返回数组中的子集合

$all代码

 > db.food.find({"fruit":{$all:["apple","banana"]}});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] } > db.food.find({"fruit":"apple"});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }

$size代码

 > db.food.find({"fruit":{$size:3}});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }

8.查询内嵌文档

用“.”表示法是查询文档区别于其他文档的主要特点。查询文档可以包含点,来表示深入内嵌文档内部。点表示法也是待插入的文档不能包含"."的原因。

 > db.food.insert({"_id":4,"fruit":{"apple":"good","banana":"good"}});
> db.food.insert({"_id":5,"fruit":{"apple":"best","banana":"good"}});
> db.food.find({"fruit.apple":"good"});
{ "_id" : 4, "fruit" : { "apple" : "good", "banana" : "good" } }
> db.food.find({"fruit.apple":"best"});
{ "_id" : 5, "fruit" : { "apple" : "best", "banana" : "good" } }

9.$where

如果前面的查询方法都不能实现,那么就轮到$where子句了,用它可以执行任意javascript作为查询的一部分,where可以接受一个javascript函数作为查询条件,并且迭代当前集合里面的所有文档,如果满足函数条件,则返回这个文档。最典型的就是比较文档中的两个键的值是否相等。

 //现在有下面这个集合,要找出库存和销量相同的记录
> db.foo.insert({"name":"apple","ku_cun":100,"xiao_liang":100});
> db.foo.insert({"name":"banana","ku_cun":120,"xiao_liang":100}); //下面是用$where查询
> db.foo.find({$where:function(){if(this.ku_cun == this.xiao_liang)return true;}
});
{ "_id" : ObjectId("533bd36909fa3b030490f059"), "name" : "apple", "ku_cun" : 100
, "xiao_liang" : 100 }

this就是引用当前集合中的一个文档。

$where除了可以接受一个function函数,还可以接受一个function的字符串

 //这是接受一个function的字符串,效果是一样的
> db.foo.find({$where:"function(){if(this.ku_cun == this.xiao_liang)return true;
}"});
{ "_id" : ObjectId("533bd36909fa3b030490f059"), "name" : "apple", "ku_cun" : 100
, "xiao_liang" : 100 }

如果return true;表示当前遍历的这个文档满足条件,那么就把这条记录(文档)返回出来。

还有更复杂的查询可以利用MapReduce

特别说明:使用$where效率比较低,因为mongodb要将BOSN数据转成javascript数据,然后一个一个遍历操作。并且不能使用索引。建议使用常规查询作为前置过滤,然后与$where组合使用。

2.游标

数据库使用游标来返回find的执行结果。客户端对游标的实现通常能够对最终结果进行有效的控制。可以限制结果的数量(limit),掠过部分数据(skip),对结果进行排序(sort),或者执行其他操作.

Skip_limit_sort代码

 //使用limit,获取查询结果的前5条记录(文档)
> db.student.find().limit(5);
{ "_id" : 1, "name" : "aa1", "age" : 11 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 } //使用skip和limit,跳过查询结果的前两条,取得后面的5条记录(文档)
> db.student.find().skip(2).limit(5);
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 4, "name" : "aa4", "age" : 14 }
{ "_id" : 5, "name" : "aa5", "age" : 10 }
{ "_id" : 6, "name" : "aa6", "age" : 11 }
{ "_id" : 7, "name" : "aa7", "age" : 12 } //使用sort,对跳过前2条之后的5条记录按照age倒序 排序
> db.student.find().skip(2).limit(5).sort({"age":-1});
{ "_id" : 3, "name" : "aa3", "age" : 13 }
{ "_id" : 8, "name" : "aa8", "age" : 13 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 1, "name" : "aa1", "age" : 11 } //使用sort,对跳过前2条之后的5条记录按照age顺序 排序
> db.student.find().skip(2).limit(5).sort({"age":1});
{ "_id" : 1, "name" : "aa1", "age" : 11 }
{ "_id" : 6, "name" : "aa6", "age" : 11 }
{ "_id" : 2, "name" : "aa2", "age" : 12 }
{ "_id" : 7, "name" : "aa7", "age" : 12 }
{ "_id" : 3, "name" : "aa3", "age" : 13 }

因为mongodb相同的键可以对应不用类型,所以在排序时有可能存在不同类型的比较。mongodb有一个预先定义好的,从小到大:

 最小值
null
数字
字符串
对象/文档
数组
二进制数据
对象ID
布尔型
日期型
时间戳
正则表达式
最大值

如果skip过多的文档,会导致速度比较慢,这是你的分页可能的想想其他方法了。例如可以根据时间排序,然后下一页时,根据上一页的最后一条记录的时间作为这一页查询的条件。

mongoDB查询及游标的更多相关文章

  1. 学习MongoDB 六: MongoDB查询(游标操作、游标信息)(三)

    一.简介 db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段.并返回到匹配文档的游标,可以随意修改查询限制.跳跃.和排序顺序的 ...

  2. MongoDB 查询分析

    MongoDB 查询分析可以确保我们建议的索引是否有效,是查询语句性能分析的重要工具. MongoDB 查询分析常用函数有:explain() 和 hint(). 使用 explain() expla ...

  3. MongoDB查询转对象是出错Element '_id' does not match any field or property of class

    MongoDB查询转对象是出错Element '_id' does not match any field or property of class   解决方法: 1.在实体类加:[BsonIgno ...

  4. MongoDB查询操作限制返回字段的方法

    这篇文章主要介绍了MongoDB查询操作限制返回字段的方法,需要的朋友可以参考下   映射(projection )声明用来限制所有查询匹配文档的返回字段.projection以文档的形式列举结果集中 ...

  5. mongodb查询文档

    说到查询,我们一般就想起了关系型数据库的查询了,比如:order by(排序).limit(分页).范围查询(大于某个值,小于某个值..,in查询,on查询,like查询等待很多),同样mongodb ...

  6. [转]mongodb 查询条件:关系运算符"$lt", "$lte", "$gt", "$gte", "$ne" 逻辑运算符"$and“, "$or“, "$nor“

    mongodb 查询条件   这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte" ...

  7. Mongodb查询的用法,备注防止忘记

    最近在用这个东西,为防止忘记,记下来. 集合简单查询方法 mongodb语法:db.collection.find()  //collection就是集合的名称,这个可以自己进行创建. 对比sql语句 ...

  8. mongodb查询关于大于小于的用法;

    mongoDB查询操作符: http://www.runoob.com/mongodb/mongodb-operators.html 项目中需要的场景是这样的,每个人每天只能领取一张明信片,换句话说, ...

  9. MongoDB查询分析

    MongoDB 查询分析可以确保我们建立的索引是否有效,是查询语句性能分析的重要工具.MongoDB 查询分析常用函数有:explain() 和 hint(). 1. explain(): 提供查询信 ...

随机推荐

  1. 网络通信TCP编程实例代码

    Makefile: all: gcc -o server server.c -lpthread gcc -o client client.c clean: rm server client serve ...

  2. Button MouseEvent颜色变化

    public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.Enter += bu ...

  3. smarty的ASSIGN()函数

    http://blog.sina.com.cn/s/blog_6721f25c01011qdj.html 主要是把程序里面的值付给模板,因为使用smarty时,模板里面是没有PHP代码的,无法显示在操 ...

  4. HTTPS连接前的几毫秒发生了什么?

    原文:http://blog.jobbole.com/48369/ 提示:英文原文写于2009年,当时的Firefox和最新版的Firefox,界面也有很大改动.以下是正文. 花了数小时阅读了如潮的好 ...

  5. 笔记:利用 Cocos2dx 3.2 与 Box2D制作一个跑酷游戏(上)

    最近写lua写得没有力气了,所以想让脑袋放松一下,刚好看到有人在用swift做游戏: Swift游戏实战-跑酷熊猫 于是脑子一短路,就想到了利用这些素材来做一个游戏. 本来不想记笔记的,但是由于选择物 ...

  6. openstack controller ha测试环境搭建记录(十四)——配置cinder(存储节点)

    先简述cinder存储节点的配置:  1.IP地址是10.0.0.41:  2.主机名被设置为block1:  3.所有节点的hosts文件已添加相应条目:  4.已经配置了ntp时间同步:  5.已 ...

  7. CodeForces 617E XOR and Favorite Number

    莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...

  8. mysql优化----第一篇:综述

    一 系统层面  查看CPU和IO状态,确定瓶颈.增 更换设备 二   数据库层面 1 参数优化. 参考文章<mysql性能优化----调整参数>增大数据库内存缓存等设置. 参考 http: ...

  9. 远离DoS攻击 Windows Server 2016发布DNS政策

             Windows Server 2016的网络功能虽然没有获得像Docker容器和Nano Server同等重要的关注,但是管理员们应该了解的是,新的域名系统(Domain Name ...

  10. iOS_init相关信息

    第一.initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的. 第二.initWithCoder 是一个类在IB中创建但 ...