前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mongoDB 逻辑运算符

mongoDB 逻辑运算符

作者头像
Leshami
发布2018-08-08 17:56:14
1.4K0
发布2018-08-08 17:56:14
举报
文章被收录于专栏:乐沙弥的世界乐沙弥的世界

在mongoDB中,逻辑运算也是较为常用的运算,这些逻辑运算通常包含与或非,取反,存在等等。本文描述mongoDB几类常用的逻辑运算符同时给出演示示例,供大家参考。

一、mongoDB中的几种逻辑运算符

代码语言:javascript
复制
    $or       逻辑或
    $and      逻辑与
    $not      逻辑非
    $nor      逻辑or的取反
    $exists   存在逻辑
    $type     查询键的数据类型

二、演示逻辑运算

演示集合persons中用到的文档数据请参考:mongoDB 比较运算符

1. $or

代码语言:javascript
复制
    Syntax: { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] } 

    db.persons.find( {$or : [{age:25},{email:"robinson.cheng@qq.com"}]})  //不同的键基于$or操作符的查询
    db.persons.find( {$or:[{age:25},{age:{$eq:27}}]})                     //相同的键基于$or操作符的查询
    db.persons.find( {age: {$in : [25,27]}})     //对于相同键的$or查询建议使用$in替换,如本查询替换上面的查询   

2. $and

代码语言:javascript
复制
    Syntax: { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

    db.persons.find( {$and: [{age:{$gt:25}},{age:{$lt:30}}]})
    db.persons.find( {$and: [{age:{$gt:25}},{"score.c":75}]})        //嵌套文档作为$and查询条件        
    db.persons.find( {$and: [{age:{$gt:25}},{books:"MONGODB"}]})     //数组作为$and查询条件 

3. $not

代码语言:javascript
复制
    Syntax: { field: { $not: { <operator-expression> } } }     

db.persons.find( { age: {$not : { $gt : 25 } } } )  //查询年龄不大于25对文档       

4. $nor

代码语言:javascript
复制
    Syntax: { $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }         

db.persons.find( {$nor : [ {age: { $gt : 25 } } ] } )                        //单个条件的$nor    
db.persons.find( {$nor : [ {age: { $gt : 25 } },{ books : "MONGODB" } ] } )  //查找age不大于25,并且书籍不包含MONGODB的文档
db.persons.find( {$or : [ {age: { $gt : 25 } },{ books : "MONGODB" } ] } )   //该查询与上正好相反,为上一个查询的补集

5. $exists

代码语言:javascript
复制
    Syntax: { field: { $exists: <boolean> } }

    //moongoDB中的exists通常是用于判断是否有这个键,而不是SQL中的某个列上存在某个值

    db.users.insert({ename:"robin",age:25})            //创建一个新的集合
    db.users.insert({ename:"henry",age:25,add:"SZ"})   //添加一个列

    db.users.find()                                    //查询集合上的记录
    { "_id" : ObjectId("57d4e95d280c7afecd0250c9"), "ename" : "robin", "age" : 25 }
    { "_id" : ObjectId("57d4e96f280c7afecd0250ca"), "ename" : "henry", "age" : 25, "add" : "SZ" }

    db.users.find( { add : { $exists : true } } )      //查询add列存在的记录
    { "_id" : ObjectId("57d4e96f280c7afecd0250ca"), "ename" : "henry", "age" : 25, "add" : "SZ" }

    db.users.find( { add : { $exists : false } } )     //查询add列不存在的记录
    { "_id" : ObjectId("57d4e95d280c7afecd0250c9"), "ename" : "robin", "age" : 25 } 

    db.users.find({$and :[ { add:{ $exists:false } },{ age : 25 } ] } )  //复合条件
    { "_id" : ObjectId("57d4e95d280c7afecd0250c9"), "ename" : "robin", "age" : 25 }   

    db.users.insert({author:"Leshami",blog:"http://blog.csdn.net/leshami"})           

6. $type //基于类型的查询

代码语言:javascript
复制
    { field: { $type: <BSON type number> | <String alias> } }
    //类似于C#/Java中的typeof

    db.users.insert({ename:"fred",age:undefined,add:"SZ"})   //age列为undefined
WriteResult({ "nInserted" : 1 })

    db.users.find({age:{$type:6}})
    { "_id" : ObjectId("57d4ed86280c7afecd0250cb"), "ename" : "fred", "age" : undefined, "add" : "SZ" }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年09月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、mongoDB中的几种逻辑运算符
  • 二、演示逻辑运算
    • 1. $or
      • 2. $and
        • 3. $not
          • 4. $nor
            • 5. $exists
              • 6. $type //基于类型的查询
              相关产品与服务
              云数据库 MongoDB
              腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档