MongoDB查询系统
首先,我们先向集合(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查询系统的更多相关文章
- 【mongodb系统学习之十】mongodb查询(一)
十.mongodb查询:find ;查询时条件中不能引用文档中其他键的值: 1).查询数据库全部数据:语法db.collectionName.find();默认只显示前20条,如图: 2).按条件查询 ...
- MongoDB 查询分析
MongoDB 查询分析可以确保我们建议的索引是否有效,是查询语句性能分析的重要工具. MongoDB 查询分析常用函数有:explain() 和 hint(). 使用 explain() expla ...
- MongoDB查询修改操作语句命令大全
MongoDB查询更新操作语句命令大全 查询操作 1.条件操作符 <, <=, >, >= 这个操作符就不用多解释了,最常用也是最简单的db.collection.find({ ...
- 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 ...
- MongoDB查询操作限制返回字段的方法
这篇文章主要介绍了MongoDB查询操作限制返回字段的方法,需要的朋友可以参考下 映射(projection )声明用来限制所有查询匹配文档的返回字段.projection以文档的形式列举结果集中 ...
- Android查询系统的音频(音乐播放器的核心)
//查询系统的音频库 public static List<MusicBean> getMusicInfo(Context context){ List<MusicBean> ...
- mongodb查询文档
说到查询,我们一般就想起了关系型数据库的查询了,比如:order by(排序).limit(分页).范围查询(大于某个值,小于某个值..,in查询,on查询,like查询等待很多),同样mongodb ...
- [转]mongodb 查询条件:关系运算符"$lt", "$lte", "$gt", "$gte", "$ne" 逻辑运算符"$and“, "$or“, "$nor“
mongodb 查询条件 这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte" ...
- Mongodb查询的用法,备注防止忘记
最近在用这个东西,为防止忘记,记下来. 集合简单查询方法 mongodb语法:db.collection.find() //collection就是集合的名称,这个可以自己进行创建. 对比sql语句 ...
随机推荐
- 九、ARM 汇编与 C 的混合编程
9.1 ARM 汇编与 C 的混合编程 9.1.1 内嵌汇编 __asm __asm("指令")例如关闭/打开总中断开关 CPSR __asm //使用 C 中变量名代替寄存器 { ...
- 组件通信 eventtBus
平级组件的通信 一个全局发布订阅模式,它是挂载到全局的 <!DOCTYPE html> <html lang="en"> <head> < ...
- vue-cli项目中引入全局scss
加载一个全局设置文件 在每个组件里加载一个设置文件,而无需每次都将其显式导入,是一个常见的需求.比如为所有组件全局使用 scss 变量.为了达成此目的: npm install sass-resour ...
- GitLab5
GitLab5发布快一个月了,决定试用下,5.0最大的特性就是用GitLab-Shell取代了Gitolite,这大大降低了安装难度,不多本人在安装过程中还是越到了一些问题,所以记录下来供要安装Git ...
- java:在Conllection接口中实际上也规定了两个可以将集合变成对象数组的操作
在Conllection接口中实际上也规定了两个可以将集合变成对象数组的操作 //在Conllection接口中实际上也规定了两个可以将集合变成对象数组的操作 List<String> a ...
- 【C】题解 (五校联考3day2)
分析 这道题看上去很恶心,实际上只用记录四坨东西就能打DP了:y坐标最小的向上射的点.y坐标最大的向下射的点.y坐标最大和最小的向右射的点,转移显然.注意,如果该状态的值为零就可以略过,否则会超时. ...
- div 垂直居中的方法
方法一 .table-body>ul>li{ font-size: 0 } .table-body>ul>li:after { content: ''; width: 0; h ...
- Mac下Sublime text3无法安装Package Control及中文乱码问题
sublime text3是一款轻量级的代码编辑器,我曾在Windows下配置过,但时间久了就忘了.这次是在mac上配置,在网上查了一些帖子,有的叙述不是很清楚,故记录一下详细过程. 在线安装: ht ...
- celery结合redis 使用
使用 Redis¶ 安装¶ 对 Redis 的支持需要额外的依赖.你可以用 celery[redis] 捆绑 同时安装 Celery 和这些依赖: $ pip install -U celery[re ...
- visual studio 2017激活
VS2017专业版和企业版激活密钥 Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB- ...