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. 查询

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