mongoDB 查询(三)

1.find()详解

db.collection.find() - 查询集合,无参数则查询所有,并返回一个游标对象

用法


    db.collection.find(query, projection)
参数


参数    描述
query    是可选参数,基于查询操作符指定了查询的条件,若希望返回collection中的所有文件,则无需指定该query 参数,直接 db.collection.find()即可。
projection    是可选参数,指定了那些字段是需要返回的,若要返回所有字段则不必要指定该参数。
projection参数的形式如下:

    { field1: , field2:  ... }
如上boolean布尔类型可以是下面的值:

1 或者 true 代表包含该field。find()方法总是包括_id字段,即便你这里没有指定要_id字段。
0或者false 代表不不包含该field。

示例:
> db.user.find()
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
> db.user.find({},{name:1})
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1" }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1" }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1" }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1" }


2.比较条件

比较操作符

$lt

<

{age:{$gte:22,$lte:27}}

$lte

<=

 
$gt

>

 
$gte

>=

 
$ne

!=

{age:{$ne:26}}

示例

> db.user.find()
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "test1", "age" : 42 }
> db.user.find({age:{$gt:30,$lt:40}})
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }


3.   $in或$nin(存在与不存在)

   

> db.user.find()
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "test1", "age" : 42 }
存在20,21的记录
> db.user.find({age:{$in:[20,21]}})
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
 
不存在20,21的记录
> db.user.find({age:{$nin:[20,21]}})
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "test1", "age" : 42 }


4.OR查询 $or

> db.user.find({$or:[{age:{$in:[20,21]}},{age:{$in:[32]}}]})
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }


5.Null

> db.user.find()
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "test1", "age" : 42 }
> db.user.find({age:{$in:[null]}})
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }

6.$not的使用

    $not可以用到任何地方进行取反操作

      查询出名字中不存在”li”的学生的信息

        db.persons.find({name:{$not:/li/i}},{_id:0,name:1})

        $not和$nin的区别是$not可以用在任何地方儿$nin是用到集合上的

7.数组查询$all

此操作符跟SQL语法的in类似,不同的是in只需要匹配括号内的某一个值,而$all必须满足括号内的所有值,如下面的代码所示:

示例


    db.users.find({age:{$all:[6,8]}});
上例可以查询出以下文档:

    {name:'David',age:26,age:[6,8,9]}
但查询不出以下这条文档:

    {name:'David',age:26,age:[6,7,9]}

9.$size - 匹配数组元素个数

它不能与比较查询符一起使用(这是弊端)

此操作符用于统计数组中的元素个数。例如,对于记录:

    {name:'David',age:26,favorite_number:[6,7,9]}
它匹配以下查询:

    db.users.find({favorite_number:{$size:3}})
但不匹配以下查询:

    db.users.find({favorite_number:{$size:2}})

10.$limit - 用来限制MongoDB聚合管道返回的文档数

> db.user.find().limit(2)
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }

11.Skip返回指定数据跨度

> db.user.find()
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "alen", "age" : 42 }
> db.user.find().skip(3)
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "alen", "age" : 42 }

12.Sort 排序
 db.user.find().sort({age:1})
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "alen", "age" : 42 }

13.Limit和Skip完成分页
> db.user.find()
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "alen", "age" : 42 }
> db.user.find().limit(3).skip(0)
{ "_id" : ObjectId("5a39f91890cf1481126c33e8"), "name" : "test1", "age" : 20 }
{ "_id" : ObjectId("5a39f92290cf1481126c33e9"), "name" : "test1", "age" : 21 }
{ "_id" : ObjectId("5a39f93190cf1481126c33ea"), "name" : "test1", "age" : 22 }
> db.user.find().limit(3).skip(3)
{ "_id" : ObjectId("5a39f9ba90cf1481126c33eb"), "name" : "test1", "address" : [ "ok", "ok", "ok2" ] }
{ "_id" : ObjectId("5a3a07b890cf1481126c33ec"), "name" : "test1", "age" : 32 }
{ "_id" : ObjectId("5a3a07bf90cf1481126c33ed"), "name" : "alen", "age" : 42 }


14.高级查询选项

    $query

    $orderby

    $maxsan:integer最多扫描的文档数

    $min:doc 查询开始

    $max:doc 查询结束

    $hint:doc  使用哪个索引

    $explain:boolean 统计

    $snapshot:boolean一致快照


参考:

http://www.mongodb.org.cn/

http://www.mongodb.org.cn/manual/34.html
--------------------- 
作者:青鸟&飞鱼 
来源:CSDN 
原文:https://blog.csdn.net/u014401141/article/details/78853017 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值