4. 查询

作者: yi_zhe | 来源:发表于2016-12-17 12:58 被阅读0次
  • 使用find进行查询

空文档查询, 匹配集合所有元素

> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>

指定条件查询

> db.persons.find({"name":"joe"})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({"name":"li"})
>

指定需要返回的键
有的时候我们不想看到不关心的键, 可以使用find或findOne的第二个参数指定
这样既能节省传输的数据量, 又能降低客户端解析数据的开销

> db.persons.find({}, {"age":1})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "age" : 26 }
>

指定不需要需要返回的键

> db.persons.find({}, {"age":0})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>
  • 查询条件

更多的查询条件: $lt, $lte, $gt, $gte

$lt, $lte, $gt, $gte分别代表less than(<), less than or equal(<=), greater than(>),
greater than or equal(>=)

以$lt为例讲一下用法

> db.persons.find({"age":{$lt:18}})
> db.persons.find({"age":{$lt:28}})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({"age":{$lt:28, $gt:25}})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({"age":{$lt:28, $gt:27}})
>

更多的查询条件: $ne

$ne代表not equal, 不仅可以用于数字, 还可以用于其他所有数据类型

> db.persons.find({"age":{$ne:28}})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({"age":{$ne:26}})
> db.persons.find({"name":{$ne:"liu"}})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>

更多的查询条件: $in/$nin和$or

$in后面跟条件数组, $nin返回与数组中所有添加都不匹配的文档

> db.persons.find({"name":{$in:["liu"]}})
> db.persons.find({"name":{$in:["liu","joe"]}})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>

$or 接受包含所有可能条件组成的数组

> db.persons.find({$or:[{"name":"joe"},{"age":26}]})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>

更多的查询条件: $not

$not 是元语句, 也就是可以用在任何其他条件之上

> db.persons.find({"name":{"$not":{$in:["joe"]}}})
> db.persons.find({"name":{"$not":{$nin:["joe"]}}})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>

条件语义

条件语句是内层文档的键, 而修改器是外层文档的键. 可以对一个键应用多个查询条件, 但是无法对一个键应用多个修改器.

有一些元操作符, 如$and, $or, $nor也位于外层文档

  • 特定类型的查询

null查询

null 不仅会匹配某个键是null的文档, 而且还会匹配缺少指定键的文档.
如果只想查询键值为null的文档, 那么还需要检查键是否已经存在

> db.persons.insert({"name":"liu", "age":28, "sex":"male", comments:null})
WriteResult({ "nInserted" : 1 })
> db.persons.find({"comments":null})
{ "_id" : ObjectId("585a822238bd2361cd7aee6e"), "name" : "liu", "age" : 28, "sex" : "male", "comments" : null }
> db.persons.find({"books":null})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
{ "_id" : ObjectId("585a822238bd2361cd7aee6e"), "name" : "liu", "age" : 28, "sex" : "male", "comments" : null }
> db.persons.find({"books":{$in:[null], $exists:true}})
> db.persons.find({"comments":{$in:[null], $exists:true}})
{ "_id" : ObjectId("585a822238bd2361cd7aee6e"), "name" : "liu", "age" : 28, "sex" : "male", "comments" : null }
>

正则表达式

MongoDB 使用Perl兼容的正则表达式

> db.persons.find({"name":/jo*/})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>

查询数组

可以指定数组中某一个元素作为查询条件, 如果需要全部匹配, 那么需要使用$all

> db.persons.find({"comments":"commentA"})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({"comments":{$all:["commentA","commentB"]}})
>

查询数组指定位置上的元素为特定值

> db.persons.find({"comments.0":"commentA"})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({"comments.1":"commentA"})
>

查询特定长度的数组

> db.persons.find({"comments":{$size:3}})
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
> db.persons.find({"comments":{$size:2}})
>

但$size不支持与$lt等查询条件一起使用

相关文章

  • 4. 查询

    使用find进行查询 空文档查询, 匹配集合所有元素 指定条件查询 指定需要返回的键有的时候我们不想看到不关心的键...

  • 6.流程执行历史记录

    1.查询历史流程实例 2.查询历史活动 3.查询历史任务 4.查询历史流程变量

  • mybatis CURD及进阶语句示例

    目录 插入 删除 修改 查询 1. 查询全部 2. 多条件查询 3. 模糊查询 4. 一对多查询 5. 多...

  • mysql基础-DQL查询(三)

    DQL查询分为:1.排序查询。2.聚合查询。3分组查询。4.分页查询 where 和having 的区别: 1.w...

  • SQL高级查询(二)

    T2 基本查询应用 1.基本查询语句 2.聚合函数 3.分组查询(重点) 4.联接查询(难点) ---------...

  • 数据库

    4.各种查询语句: 查询时指定别名: 1) selete id AS '编号',name AS '姓名' from...

  • SQL 查询命令

    一 数据库基本操作 二 高级查询 1. 新建表结构 2. 聚合查询 3. 多表查询 4. 连接查询 关于INNER...

  • MySQL-6:查询语句

    1、DQL:查询语句: 1.排序查询 2.聚合函数 3.分组查询 4.分页查询 2、约束3、多表之间的关系4、范式...

  • SQL高级查询(三)

    T3 子查询的应用(一) 1.子查询的概述 2.单行子查询 3.多行子查询(重点) 4.子查询在select子句中...

  • 笔记-MySQL常用CRUD

    1.插入数据: .插入单行: .插入多行: 2.删除数据: 3.更新数据: 4.查询: .普通查询 .分组查询记录...

网友评论

      本文标题:4. 查询

      本文链接:https://www.haomeiwen.com/subject/ugvnmttx.html