首先,我们先向集合(collections)中添加测试文档(documents)。如下:

> for(i=1;i<=5;i++) db.test.insert({x:i,y:i*i,z:6-i})
WriteResult({ "nInserted" : 1 })

  1、下面查看我们插入的数据:

> db.test.find()
{ "_id" : ObjectId("59361be7c718d6f408c6cae5"), "x" : 1, "y" : 1, "z" : 5 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae6"), "x" : 2, "y" : 4, "z" : 4 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae7"), "x" : 3, "y" : 9, "z" : 3 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae8"), "x" : 4, "y" : 16, "z" : 2 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae9"), "x" : 5, "y" : 25, "z" : 1 }

  2、只查看部分数据字段

> db.test.find({},{_id:0,x:1})
{ "x" : 1 }
{ "x" : 2 }
{ "x" : 3 }
{ "x" : 4 }
{ "x" : 5 }(其中,第一个{}中是筛选条件,第二个{}中是决定哪些数据字段显示,即过滤器,0表示不显示,1表示显示)

  3、按指定条件筛选数据

> db.test.find({x:1},{_id:0})
{ "x" : 1, "y" : 1, "z" : 5 }

  4、比较部分

操作

格式

范例

等于

{<key>:<value>}

db.col.find({x:1,y:1}).pretty()

小于

{<key>:{$lt:<value>}}

db.col.find({x:{$lt:3}}).pretty()

小于或等于

{<key>:{$lte:<value>}}

db.col.find({x:{$lte:3}}).pretty()

大于

{<key>:{$gt:<value>}}

db.col.find({x:{$gt:3}}).pretty()

大于或等于

{<key>:{$gte:<value>}}

db.col.find({x:{$gte:3}).pretty()

不等于

{<key>:{$ne:<value>}}

db.col.find({x:{$ne:1}}).pretty()

范围选择1

{<key>:{$lt:<val>,$gte:<val>}}

db.test.find({x:{$lt:4,$gte:2}})

范围选择2

{<key>:{$in:[value1,value2]}}

db.test.find({x:{$in:[1,2]}})

范围选择3

{<key>:{$nin:[value1,value2]}}

db.test.find({x:{$nin:[1,2]}})

  其中,$nin、$ne等是一种比较低效的查询选择器,它们会进行全表扫描,因此,最好不要单独使用。并且单独使用$ne也不会利用索引的优势,非常低效。

  5、And与Or

操作

格式

范例

效果

And

{$and:[{<key>:<value>},{}....]}

db.test.find({$and:[{x:2},{y:4}]},{_id:0})

{ "x" : 2, "y" : 4, "z" : 4 }

Or

{$or:[{<key>:<value>},{}....]}

db.test.find({$or:[{x:2},{y:1}]},{_id:0})

{ "x" : 1, "y" : 1, "z" : 5 }

{ "x" : 2, "y" : 4, "z" : 4 }

And Or

省略

db.test.find({x:{$lt:4},$or:[{y:4},{z:5}]},{_id:0})

{ "x" : 1, "y" : 1, "z" : 5 }

{ "x" : 2, "y" : 4, "z" : 4 }

  6、exists

    在mongodb中$exists与关系数据库中的exists不一样,因为在MongoDB中表(集合collection)结结构不是固定的,有的时候需要返回包含有某个字段的所有记录或者不包含某个字段的所有字段,$exists这时就可以派上用场了。此处我们为了测试就在插入一条数据:

> db.test.insert({x:1,y:1,flag:true})
WriteResult({ "nInserted" : 1 })

  下面我们假设需要访问包含flag字段的文档,如下:

> db.test.find({flag:{$exists:true}})
{ "_id" : ObjectId("59364d6561dce208b23fc306"), "x" : 1, "y" : 1, "flag" : true }

  当然,为了实现这种需求,我们也可以用另外一种语句来代替$exists

> db.test.find({flag:{$ne:null}})
{ "_id" : ObjectId("59364d6561dce208b23fc306"), "x" : 1, "y" : 1, "flag" : true }

  7、嵌套查询

  下面,向test中添加一个嵌套形式的数据,即某个字段的值也是一个BSON对象

> db.test.insert({id:1,name:"xiaoming",detail:{sex:"male",age:20}})
WriteResult({ "nInserted" : 1 })

> db.test.find({"detail.age":20}).pretty()
{
"_id" : ObjectId("5936507961dce208b23fc307"),
"id" : 1,
"name" : "xiaoming",
"detail" : {
"sex" : "male",
"age" : 20
}
}

  嵌套查询时匹配的key如果有多级嵌套深度,就一级一级地用点号展开

  8、数组操作

> db.test.insert({id:1,name:"xiaoliang",detail:[{sex:"male",age:22},{address:"china",post:5}]})
WriteResult({ "nInserted" : 1 })

> db.test.find({"detail.1.post":5}).pretty()
{
"_id" : ObjectId("593655b461dce208b23fc308"),
"id" : 1,
"name" : "xiaoliang",
"detail" : [
{
"sex" : "male",
"age" : 22
},
{
"address" : "china",
"post" : 5
}
]
}

匹配字符串中先取需要匹配的key(detail),由于detail键对应的value为数组,detail.1表示要取数组中的第2个位置处的元素,又由于数组的元素有是个BSON文档对象,因此,我们可以通过detail.1.post定位到需要匹配的键。

  9、查询最新数据

> db.ttt.find().pretty()
{
"_id" : ObjectId("593270bd0976e8f92b2d4514"),
"id" : 1,
"StatusInfo" : [
{
"status" : 9,
"desc" : "已取消"
},
{
"status" : 2,
"desc" : "已付款"
}
]
}

这是我们的测试数据,在现实需求中我们经常遇到需要查询的是最新数据,对于数据类型,我们有专门的操作符$silce来完成

> db.ttt.find({id:1},{_id:0,"StatusInfo":{"$slice":-1},"StatusInfo.desc":1})
{ "StatusInfo" : [ { "desc" : "已付款" } ] }

  10、正则查询

> db.test.find({age:20})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }

查询"name"以"zhan"开头的数据:

> db.test.find({name:{$regex:"zhan"}})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }

当然还是可以这样的:

> db.test.find({name:/zhan/})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }
> db.test.find({name:/han/})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }

忽略大小写查询

> db.test.find({name:{$regex:"zhan",$options:"$i"}})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }
{ "_id" : ObjectId("59365f1161dce208b23fc30b"), "name" : "ZHANbsd", "age" : 22 }
> db.test.find({name:/zhan/i})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }
{ "_id" : ObjectId("59365f1161dce208b23fc30b"), "name" : "ZHANbsd", "age" : 22 }

MongoDB查询系统的更多相关文章

  1. 【mongodb系统学习之十】mongodb查询(一)

    十.mongodb查询:find ;查询时条件中不能引用文档中其他键的值: 1).查询数据库全部数据:语法db.collectionName.find();默认只显示前20条,如图: 2).按条件查询 ...

  2. MongoDB 查询分析

    MongoDB 查询分析可以确保我们建议的索引是否有效,是查询语句性能分析的重要工具. MongoDB 查询分析常用函数有:explain() 和 hint(). 使用 explain() expla ...

  3. MongoDB查询修改操作语句命令大全

    MongoDB查询更新操作语句命令大全 查询操作 1.条件操作符 <, <=, >, >= 这个操作符就不用多解释了,最常用也是最简单的db.collection.find({ ...

  4. MongoDB查询转对象是出错Element '_id' does not match any field or property of class

    MongoDB查询转对象是出错Element '_id' does not match any field or property of class   解决方法: 1.在实体类加:[BsonIgno ...

  5. MongoDB查询操作限制返回字段的方法

    这篇文章主要介绍了MongoDB查询操作限制返回字段的方法,需要的朋友可以参考下   映射(projection )声明用来限制所有查询匹配文档的返回字段.projection以文档的形式列举结果集中 ...

  6. Android查询系统的音频(音乐播放器的核心)

    //查询系统的音频库 public static List<MusicBean> getMusicInfo(Context context){ List<MusicBean> ...

  7. mongodb查询文档

    说到查询,我们一般就想起了关系型数据库的查询了,比如:order by(排序).limit(分页).范围查询(大于某个值,小于某个值..,in查询,on查询,like查询等待很多),同样mongodb ...

  8. [转]mongodb 查询条件:关系运算符"$lt", "$lte", "$gt", "$gte", "$ne" 逻辑运算符"$and“, "$or“, "$nor“

    mongodb 查询条件   这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte" ...

  9. Mongodb查询的用法,备注防止忘记

    最近在用这个东西,为防止忘记,记下来. 集合简单查询方法 mongodb语法:db.collection.find()  //collection就是集合的名称,这个可以自己进行创建. 对比sql语句 ...

随机推荐

  1. GUI学习之二十七——布局管理学习总结

    今天讲一个大的内容——布局管理. 一.布局管理的诞生背景 在前面所讲的所有案例中,我们都是用采用手动布局的方式来布局的.结合个案例来说明一下:在一个界面上放三个label,三个label纵向排列 fr ...

  2. bean获取Spring容器

    Person.java public class Person implements ApplicationContextAware{ private String name; private int ...

  3. HBase过滤器(转载)

    HBase为筛选数据提供了一组过滤器,通过这个过滤器可以在HBase中的数据的多个维度(行,列,数据版本)上进行对数据的筛选操作,也就是说过滤器最终能够筛选的数据能够细化到具体的一个存储单元格上(由行 ...

  4. 【leetcode】1095. Find in Mountain Array

    题目如下: (This problem is an interactive problem.) You may recall that an array A is a mountain array i ...

  5. 微信小程序-wxml-空格

    必须要在<text>标签中 先在标签中写decode="{{true}}"然后 就代表空格了   占一个中文字符

  6. 关于vs2019

    一.vs2019中的MFC 在想创建一个基于对话的应用时找不着模版了,这下可慌了,试遍了已有的各个模版都没要,要么就是缺少头文件,我在想是不是少安装了什么选项.重装了相关模块,最后又核对了一遍,都对. ...

  7. Spring——框架

    [定义] 框架就是制定一套规则或规范(思想),大家(程序员)在该规范或者规则(思想)下工作. 或者说是使用别人搭好的舞台,你来表演. [特点] ——半成品 ——封装了特定的处理流程和控制逻辑 ——成熟 ...

  8. Ubuntu安装过程中的问题

    1.win10系统安装32bit ubuntu,使用VM安装ubuntu 的iso文件,刚启动不停按F2,进入BIOS,boot设置为 CD-ROM drive 2.安装界面都没有出现,电脑老是重启, ...

  9. CF1019E Raining season

    https://www.luogu.org/problemnew/show/CF1019E 题解 \[ dis=day*a+b \] \[ b=-day*a+dis \] 然后就变成了斜率优化. 考虑 ...

  10. [design pattern](2) Observer

    前言 在上一个博客中我们介绍了Strategy模式,它是行为型模式麾下的一员大将.那么本博客我们来学习一下行为型模式麾下的另一员大将Observer模式. 思考题 老套路,先来思考下面的问题: 问题: ...