MongDB4.0-入门学习之运算符

MongDB 4.0 入门学习之运算符

基本语法:db.collection.find({<key>:{$symbol:<value>}})

条件查询匹配运算符

符号 描述 范例 js释义
$eq 等于 {qty:{$eq:2}} or {qty:2} qty===2
$gt 大于 {qty:{$gt:2}} qty>2
$gte 大于或等于 {qty:{$gte:2}} qty>=2
$lt 小于 {qty:{$lt:2}} qty<2
$lte 小于或等于 {qty:{$lte:2}} qty<=2
$ne 不等于 {qty:{$ne:2}} qty!=2
$in 查询等于指定数组中任何值的数据 {qty:{$in:[5,2,3]}} `qty===5
$nin 查询不等于指定数组中任何值数据 {qty:{$nin:[5,2,3]}} `qty!=5

逻辑运算符

  • $and 逻辑且

    • 语法: {$and:[{<expression1>}, {<expression2>}, ... ,{<expressionN>}]}
    • 范例: {$and:[{qty:{$ne:2}},{"name":{$eq:"测试"}}]}
    • 范例js释义: qty!=2 && "name"==="测试"
  • $not 逻辑非

    • 语法: {<key>:{$not:{<operator-expression>}}}
    • 范例: {price:{$not:{$gt:1.99}}}
    • 范例js释义: !(price>1.99)
  • $nor 逻辑非或

    • 语法: {$nor:[{<expression1>}, {<expression2>}, ...,{<expressionN>}]}
    • 范例: {$nor:[{price:1.99}, {sale:true}]}
    • 范例js释义: !(price===1.99||sale===true)
  • $or 逻辑或

    • 语法: {$or:[{<expression1>}, {<expression2>}, ...,{<expressionN>}]}
    • 范例: {$or:[qty:{$lt:20}}, {price:10}]}
    • 范例js释义: qty<20 || price===10

检测运算符

  • $exists 查询值是否存在

    • 语法: {<key>:{$exists:<boolean>}}
    • 范例: {qty:{$exists:true, $nin:[ 5, 15 ]}}
    • 范例js释义: qty && (qty!=5 || qty!=15)
  • $type 检测值的类型

    • 语法: {<key>:{$type:<BSON type>}}
    • 范例: {"zipCode":{$type:2}}} or {"zipCode":{$type:"string"}}}
    • 范例js释义: typeof "zipCode" === "string"
    • 数据类型请自行到官网文档查询 MongoDB Operator $type
posted @ 2019-06-21 17:38  YanEr、  阅读(217)  评论(0编辑  收藏  举报