zoukankan      html  css  js  c++  java
  • 《mongoDB》查询

    一:简单查询

    • db.collection.find(query, projection)
        - query :可选,使用查询操作符指定查询条件
        - projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。
    • 查询时使用 .pretty() 可以使代码格式化显示,类似于 G
    • find() 语句不加 QUERY 条件时,默认查询集合文档,默认显示20条
    • 如果只想找到一条记录,请使用 findOne()
      > db.demo.findOne({"name":"lisi"})
      {
          "_id" : ObjectId("5c6158fa2f5e0916de3c790e"),
          "name" : "lisi",
          "age" : 26
      }
    • projection 可以选择字段是否展示 1为展示/0不展示
      find()/findOne()同理
      
      > db.demo.findOne({"name":"lisi"}, {"_id":0,"name":1})
      { "name" : "lisi" }

    二:查询条件查询

    • 适用的查询条件有
      • $lt    小于
      • $lte  小于等于
      • $gt   大于
      • $gte 大于等于
    • 找到 年龄(age) 小于30 大于25的文档
      > db.demo.find({"age" : {"$lt" : 30, "$gt" : 25}})
      { "_id" : ObjectId("5c6158eb2f5e0916de3c790c"), "name" : "zhangsan", "age" : 26 }
      { "_id" : ObjectId("5c6158fa2f5e0916de3c790d"), "name" : "zhangsan", "age" : 26 }
      { "_id" : ObjectId("5c6158fa2f5e0916de3c790e"), "name" : "lisi", "age" : 26 }
      { "_id" : ObjectId("5c61590f2f5e0916de3c790f"), "name" : "zhangsan", "age" : 26 }
      { "_id" : ObjectId("5c61590f2f5e0916de3c7910"), "name" : "lisi", "age" : 26 }

    三:OR 查询

    • $in 可以查询一个键的多个值
    • > db.demo.find({"age" : {"$in":[20,26]}})
      { "_id" : ObjectId("5c613f942f5e0916de3c7909"), "age" : 20 }
      { "_id" : ObjectId("5c6158eb2f5e0916de3c790c"), "name" : "zhangsan", "age" : 26 }
      { "_id" : ObjectId("5c6158fa2f5e0916de3c790d"), "name" : "zhangsan", "age" : 26 }
      { "_id" : ObjectId("5c6158fa2f5e0916de3c790e"), "name" : "lisi", "age" : 26 }
      { "_id" : ObjectId("5c61590f2f5e0916de3c790f"), "name" : "zhangsan", "age" : 26 }
      { "_id" : ObjectId("5c61590f2f5e0916de3c7910"), "name" : "lisi", "age" : 26 }
    • OR 更通用,可以查询更多的条件
    • > db.demo.find({"$or" : [{"age" : 20}, {"name":"lihong"}]})
      { "_id" : ObjectId("5c613f942f5e0916de3c7909"), "age" : 20 }
      { "_id" : ObjectId("5c61587a2f5e0916de3c790b"), "name" : "lihong", "age" : 25 }

    四:null 查询

    • null 的匹配是有些奇怪,不仅仅会匹配某个键为null的文档,还会匹配不包含这个键的文档
    • > db.demo.find({"age" : null})
      { "_id" : ObjectId("5c61011d0a485d689a5f3e15"), "c" : "d" }
      { "_id" : ObjectId("5c6147c92f5e0916de3c790a"), "age" : null }
    • 如果只是想匹配 null 的存在,还需要 使用 exists 来判断键值是否存在
    • db.demo.find({"age" : {"$in" : [null], "$exists" : true}})
      { "_id" : ObjectId("5c6147c92f5e0916de3c790a"), "age" : null }

    五:数组

    • 数组的查询和标量的查询类似
      • 插入
        > db.demo.insert({"fruit" : ["banana", "apple", "origin"]});
        WriteResult({ "nInserted" : 1 })
      • 查询
        > db.demo.find({"fruit" : "apple"})
        { "_id" : ObjectId("5c6259632f5e0916de3c7912"), "fruit" : [ "banana", "apple", "origin" ] }
    • 匹配多个元素的数组
      • > db.demo.find({"fruit" : {$all :["banana", "apple"]}});
        { "_id" : ObjectId("5c6259632f5e0916de3c7912"), "fruit" : [ "banana", "apple", "origin" ] }
        { "_id" : ObjectId("5c6259de2f5e0916de3c7913"), "fruit" : [ "banana", "apple" ] }
    • 根据数组长度匹配
      • > db.demo.find({"fruit" : {$size : 3}});
        { "_id" : ObjectId("5c6259632f5e0916de3c7912"), "fruit" : [ "banana", "apple", "origin" ] }

    六:查询内嵌文档

    七:$where 查询

    • 避免使用,英文在查询时需要把文档从 BSON 转为 Javascript 对象操作

    八:limit/skip/sort

    • limit
      • limit 指定为上限
      • db.demo.find().limit(3);
    • skip
      • 跳过前 n 个匹配的文档
      • 一次略过过多会导致性能问题,用排序或者其他方法解决
      • db.demo.find().skip(3);
    • sort
      • 接受一对键值对作为排序,1升序/-1降序,如果指定多个键,则按照顺序进行排序
      • > db.demo.find().sort({"age": -1, "name":1});
        { "_id" : ObjectId("5c6158fa2f5e0916de3c790e"), "name" : "lisi", "age" : 26 }
        { "_id" : ObjectId("5c61587a2f5e0916de3c790b"), "name" : "lihong", "age" : 25 }
        { "_id" : ObjectId("5c613f942f5e0916de3c7909"), "age" : 20 }
      • 对于混合数据的比较,由大到小

  • 相关阅读:
    Git回退---reset和revert
    XML解析
    SpringBoot学习day01
    Spring boot精要
    JS没有contains方法,可以用indexof实现
    git fetch 取回所有分支(branch)的更新(转)
    idea 设置注释模板
    git退出编辑模式
    git 提交代码到远程分支
    linux下,保存退出vim编辑器(转)
  • 原文地址:https://www.cnblogs.com/25-lH/p/10364781.html
Copyright © 2011-2022 走看看