zoukankan      html  css  js  c++  java
  • MongoDB-查询

    常用查询操作符:

    1、比较运算符

    $eq:等于

    db.stars.find({"name":"A-Lin"})
    db.stars.find({"name":{"$eq":"A-Lin"}});

    $gt:大于

    db.stars.find({"age":{"$gt":30}})

    $gte:大于等于

    db.stars.find({"age":{"$gte":30}})

    $lt:小于

    db.stars.find({"age":{"$lt":30}})

    $lte:小于等于

    db.stars.find({"age":{"$lte":30}})

    $ne:不等于

    db.stars.find({"age":{"$ne":30}})

    $in:查找数组范围内的指定值

    db.stars.find({"age":{"$in":[22,40]}})

    $nin:查找不在数组范围内的值

    db.stars.find({"age":{"$nin":[22,40]}})

    2、逻辑运算符

    $or:或运算符,语法:

    { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

    示例:

    db.stars.find({"$or":[{"name":"A-Lin"},{"age":40}]});

    $and:与运算符,语法:

    { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

    示例

    db.stars.find({"$and":[{"name":"A-Lin"},{"age":22}]});

    $not:取反操作符,语法

    { field: { $not: { <operator-expression> } } }

    示例

    db.stars.find({"name":{"$not":{"$eq":"A-Lin"}}})

    $nor:测试所有的查询条件,都失败的文档将会返回(类似于对数组中的查询条件做OR操作,然后取反),语法

    { $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }

    示例

    db.stars.find({"$nor":[{"name":"A-Lin"},{"age":"20"}]})

    3、其他操作符

    $exists:查询所有存在指定字段的文档,语法

    { field: { $exists: <boolean> } }

    示例

    db.stars.find({"age":{"$exists":true}})

    $type:查询指定字段为指定类型的文档

    db.stars.find({"age":{"$type":"double"}})

    $mod:求余操作符,语法

    { field: { $mod: [ divisor, remainder ] } }

    示例

    //对4取模,余数为0
    db.stars.find({"age":{"$mod":[4,0]}})

    $regex:正则表达式,语法

    { <field>: { $regex: /pattern/, $options: '<options>' } }
    { <field>: { $regex: 'pattern', $options: '<options>' } }
    { <field>: { $regex: /pattern/<options> } }

    简写语法

    { <field>: /pattern/<options> }

    示例

    //查找简介中含有女歌手关键字的文档
    db.stars.find({"Desc":/女歌手/})

    $text:全文检索,语法

    {
      $text:
        {
          $search: <string>,
          $language: <string>,
          $caseSensitive: <boolean>,
          $diacriticSensitive: <boolean>
        }
    }

    $where:使用JavaScript语法查找文档

    db.stars.find({"$where":function()
    {
        return this.age>=20;
    }})

    4、数组操作符

    $all:查找一个数组,该数组包含所有给定的值,语法

    { <field>: { $all: [ <value1> , <value2> ... ] } }

    示例

    db.stars.find({"tags":{"$all":["软妹子","女人味"]}});

    $elemMatch:查找一个数组,该数组中的元素包含匹配所有给定的查询条件,语法

    { <field>: { $elemMatch: { <query1>, <query2>, ... } } }

    示例

    //查找tags数组,返回数组中包含["女人味","软妹子"]的所有文档
    db.stars.find({"tags":{"$elemMatch":{"$in":["女人味","软妹子"]}}});

    $size:查找指定长度数组的文档

    //查找tags数组长度为4的文档
    db.stars.find({"tags":{"$size":4}});

    $:操作符

    $slice:操作符

    find:查询方法

     db.collection.find()

  • 相关阅读:
    Kappa Architecture: A Different Way to Process Data
    Lambda architecture and Kappa architecture
    Questioning the lambda architecure
    Lambda Architecture: Achieving Velocity and Volume with Big Data
    beego 参数配置
    hadoop 3.1.1 安装
    Idea 切换git账号
    IntelliJ IDEA 开发git多模块项目
    打印1到1亿的平方
    IDEA 逆向工程
  • 原文地址:https://www.cnblogs.com/Jabben_Yi/p/4931358.html
Copyright © 2011-2022 走看看