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与正则表达式联合使用时极为有效,用来查找那些与特定模式不匹配的文档。

 
 

mongo-查询(2)——比较/$in/$nin/$or/$not的更多相关文章

  1. Mongo 查询

    Mongo 查询   mongo js 遍历 db.getCollection('CPU').find({}).limit(100).sort({"time":-1}).forEa ...

  2. Mongo查询百万级数据性能问题及JAVA优化问题

    Mongo查询百万级数据  使用分页  skip和limit 效率会相当慢   那么怎么解决呢  上代码 全部查询数据也会特别慢 Criteria criteria = new Criteria(); ...

  3. 一次mongo查询不存在字段引发的事故

    话说今天的一个小小的查询失误给了我比较深刻的教训,也让我对mongo有了更深刻的理解,下面我们来说说这个事情的原委: 我们经常使用阿里云子账号在DMS上查询线上数据库数据,今天也是平常的一次操作 集合 ...

  4. 整理最近用的Mongo查询语句

    背景 最近做了几个规则逻辑.用到mongo查询比较多,就是查询交易信息跑既定规则筛选出交易商户,使用聚合管道进行统计和取出简单处理后的数据,用SQL代替业务代码逻辑的判断. 方法 MongoDB聚合使 ...

  5. 在shell 上执行mongo 查询

    需求 在写小工具的时候,经常遇到需要从mongodb 里面查东西来用,因为要跟其他bash 工具链结合在一起用,所以最理想的方法是能够在shell 上执行查询,然后pipe 给接下来的工具做处理. 方 ...

  6. laraver mongo 查询操作

    1,mongo 不支持特殊where条件(&,|) 2,mongo 可以连接mysql的表查询,但不支持连表的where查询

  7. mongo查询日期格式数据

    /ali/mongodb/bin/mongo -u user -p '123456' 127.0.0.1:27017/KYElog ISODate方式 db.col_02.find({"Lo ...

  8. mongo 查询 距离 某个点 多少 米距离 感谢 提供的数据。 感谢 mvc的 demo 。反正 就是各种感谢 文档之类的。

    昨天 去面试来着, 问了一下mong . 我记得mong支持 地理位置索引的,说了一下. 然后 面试官说 查询某个点 的 多少米范围, 这个该怎么实现? 我懵逼了.... 回去 查询了一下. 发现有 ...

  9. Mongo查询list数组中一个字段大于特定条数

    由于刚刚接触mongodb,很多语法还不是很了解,写的不好的地方请大佬指出 查询的demo数据 { "_id":Object("xxxxxxxx"), &quo ...

  10. 如何将mongo查询结果导出到文件中

    1.新建一个js文件,将查询方法写进去,如dump.js,文件内容如下 var c = db.campaign.find({status:1}).limit(5) while(c.hasNext()) ...

随机推荐

  1. 云从科技 OCR任务 pixel-anchor 方法

    云从科技提出了一种端到端的深度学习文本检测框架Pixel-Anchor,通过特征共享的方式高效的把像素级别的图像语义分割和锚检测回归放入一个网络之中, 把像素分割结果转换为锚检测回归过程中的一种注意力 ...

  2. 为训练深度OCR 图像,生成文本图像

    https://github.com/Sanster/text_renderer Generate text images for training deep learning ocr model 在 ...

  3. Oracle学习笔记之二,Oracle 11g体系结构

    Oracle 11g体系结构概述 实例(Instance),是指一组Oracle后台进程以及在服务器中分配的共享内存区域: 数据库(Database),是由基于磁盘的数据文件.控制文件.日志文件.参数 ...

  4. 通达OA 在工作流中直接查询表单内容的开发尝试(图文)

    一个朋友提出要在工作里直接查询表单内容的需求,原来他们把工作流当做业务系统来用.也算把工作流用到极致了.为了实现像软件里直接的查询功能,他想在办理工作流的时候直接能查询到表单里面的内容. 通过研究通达 ...

  5. leetcode第一刷_Jump Game II

    要求最小的步数,是不是非常easy想到用dp啊? 我一開始的做法是,当找到了一个可以从它延伸到更远的位置,就把这个位置和最远位置的步数都更新一下,结果超时了. 事实上这样不仅是超时的,并且是错误的.由 ...

  6. tortoisegit 7步 学会git分支

    tortoisegit 7步 学会git分支 做自己的产品,分支还是很重要的.例如,我发布了一个app,但是有bug,又想做新功能,怎么办呢?如果只在一个git上开发的话,bug会越来越多,原来bug ...

  7. 工作流模式 (zhuan)

    http://www.cnblogs.com/soundcode/archive/2010/12/30/1922283.html *********************************** ...

  8. Brocade300 commands

    aaaconfig                  Configure RADIUS for AAA servicesad                         Specifies all ...

  9. 获取Django中model字段名 字段的verbose_name

    obj._meta.fields 为关键 obj为model类 推荐使用函数 from django.apps import apps def getmodelfield(appname,modeln ...

  10. response.setContentType 与response.setCharacterEncoding

    response.setContentType  设置发送到客户端的响应的内容类型,可以包括字符编码说明.  也就是说在服务器端坐了这个设置,那么他将在浏览器端起到作用,在你打开浏览器时决定编码方式 ...