• mongo-查询(2)——比较/$in/$nin/$or/$not


     

    mongo通find来查找文档。可以执行精确匹配和模糊匹配。

    2. 模糊匹配

    2.1比较

    > $gt , >= $gte, < $lt, <= $lte, != $ne

    > db.tianyc02.find()
    { "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
    { "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
    > db.tianyc02.find({age:{$lt:100}})
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
    > db.tianyc02.find({age:{$lt:100,$gt:20}})
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
    > db.tianyc02.find({age:{$ne:11}})
    { "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
    { "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }

    2.2 $in & $nin

    > db.tianyc02.find()
    { "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
    { "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
    > db.tianyc02.find({age:{$in:[11,22]}})
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
    > db.tianyc02.find({age:{$nin:[11,22]}})
    { "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
    { "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }

    2.3 $or

    > db.tianyc02.find({$or:[{age:11},{age:22}]})
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
    > db.tianyc02.find({$or:[{age:11},{name:'xttt'}]})
    { "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
    { "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }

    2.4 $not

    > db.tianyc02.find({age:{$mod:[11,0]}})
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
    { "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }
    > db.tianyc02.find({age:{$not:{$mod:[11,0]}}})
    { "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
    { "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }

    $mod会将查询的值除以第一个给定的值,若余数等于第二个给定的值,则返回该结果。

    $not与正则表达式联合使用时极为有效,用来查找那些与特定模式不匹配的文档。

     
     
  • 相关阅读:
    黑客网站拒绝删除微软机密文件 被迫关停 狼人:
    微软:SE2010不是我们的杀毒软件 狼人:
    Websense互联网威胁报告: Web威胁更具混合性 狼人:
    安全厂商:至少100家企业遭遇谷歌同样攻击 狼人:
    金山毒霸:极虎继续发威 病毒借兽兽门大肆传播 狼人:
    网站站点百度再出新声明 内容始终是王道
    目标合并【C++】容器元素的复制和变换
    设置配置高可用的Mysql双机热备(Mysql_HA)
    百度高度js 检测web页面是否正在操作,如超过规定时间内未操作将自动跳转至指定页面。
    删除元素【C++】顺序容器 Vector 注意事项
  • 原文地址:https://www.cnblogs.com/williamjie/p/9943733.html
Copyright © 2020-2023  润新知