mongodb高级操作(2)-查询
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongodb高级操作(2)-查询,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5441字,纯文字阅读大概需要8分钟。
内容图文
1.查询文档find介绍 mongodb中使用find来进行查询.find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件.如果不指定条件默认就是{},那么就是查询所有文档. db . test . find () { _id : ObjectId ( 573c858c323f7f2e2ccb0e17 ), name
1.查询文档find介绍
mongodb中使用find来进行查询.find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件.
如果不指定条件默认就是{},那么就是查询所有文档.
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1, "status" : "done" }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10, "status" : "done" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
向查询中指定键值,意味着限定查询条件,查询简单的类型只要指定要查找的值就行了:
> db.test.find({"name":"brent"})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
可以在查询中指定多个键值对,以逗号隔开,这样的意思是条件1 AND 条件2 AND ....的意思:
> db.test.find({"name":"brent","age":43})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
指定返回的键
有时并不是需要返回所有的键,这时可以通过指定find或者findOne的第二个参数来指定想要的键,例如只想查询"name"为"brent"的name和age键:
> db.test.find({"name":"brent"},{"name":1,"age":1})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30 }
默认情况下_id都是会返回的,也可以使用第二个参数来剔除某个键,例如我们不希望得到status的键:
> db.test.find({"name":"brent"},{"status":0})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30 }
使用这种方法还可以将_id列剔除:
> db.test.find({"name":"brent"},{"status":0,"_id":0})
{ "name" : "brent", "age" : 43 }
{ "name" : "brent", "age" : 30 }
限制
查询传递的参数必须是常量,例如如果想查询一个文档中的某两个列相等的情况是不行的.
2.查询条件
查询除了上面简单介绍的精确匹配,还有更加复杂的查询,比如范围查询,or,and,取反等等
查询条件
"$lt","$lte","$gt","$gte"就是全部的比较操作符.分别对应着<,<=,>和>=,可以将这些组合起来以便查找一个范围的值.
例如下面的例子查找age大于20小于30的文档:
> db.test2.find()
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
> db.test2.find({"age":{"$gt":20,"$lt":30}})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
对于文档的键值不等于某个特定的值,就要使用"$ne"了,他表示不相等.下面例子要查找name不等于brent的用户:
> db.test2.find({"name":{"$ne":"brent"}})
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
OR查询
有两种方式进行OR查询:"$in"可以用来查询一个键的多个值,"$or"可以在多个键中查询任意给定的值.
例如下面要查询age为10,14的文档:
> db.test2.find({"age":{"$in":[10,14]}})
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
使用"$in"的时候,可以指定不同类型的条件和值,如果"$in"的数组只有一个值,那么和直接匹配是一样的."$in"和"nin"是相对的,"nin"返回数组中不匹配的文档.查询age不为10,14的文档:
> db.test2.find({"age":{"$nin":[10,14]}})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
"$in"只能对单个键做OR查询,如果想对多个键做匹配OR查询那么可以使用"$or","$or"接受一个包含所有可能的数组作为条件.
例如下面这个查询name为bob或者age为10的文档:
> db.test2.find({"$or":[{"name":"bob"},{"age":10}]})
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
还可以将"$or"和"in"联合起来使用:
> db.test2.find({"$or":[{"age":{"$in":[10,28]}},{"name":"bob"}]})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age"
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1, "status" : "done" }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10, "status" : "done" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
> db.test.find({"name":"brent"})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
> db.test.find({"name":"brent","age":43})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30 }
> db.test.find({"name":"brent"},{"status":0})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30 }
> db.test.find({"name":"brent"},{"status":0,"_id":0})
{ "name" : "brent", "age" : 43 }
{ "name" : "brent", "age" : 30 }
查询条件
"$lt","$lte","$gt","$gte"就是全部的比较操作符.分别对应着<,<=,>和>=,可以将这些组合起来以便查找一个范围的值. 例如下面的例子查找age大于20小于30的文档:> db.test2.find()
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
对于文档的键值不等于某个特定的值,就要使用"$ne"了,他表示不相等.下面例子要查找name不等于brent的用户:> db.test2.find({"age":{"$gt":20,"$lt":30}})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
> db.test2.find({"name":{"$ne":"brent"}})
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
OR查询
有两种方式进行OR查询:"$in"可以用来查询一个键的多个值,"$or"可以在多个键中查询任意给定的值. 例如下面要查询age为10,14的文档:使用"$in"的时候,可以指定不同类型的条件和值,如果"$in"的数组只有一个值,那么和直接匹配是一样的."$in"和"nin"是相对的,"nin"返回数组中不匹配的文档.查询age不为10,14的文档:> db.test2.find({"age":{"$in":[10,14]}})
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
"$in"只能对单个键做OR查询,如果想对多个键做匹配OR查询那么可以使用"$or","$or"接受一个包含所有可能的数组作为条件. 例如下面这个查询name为bob或者age为10的文档:> db.test2.find({"age":{"$nin":[10,14]}})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
还可以将"$or"和"in"联合起来使用:> db.test2.find({"$or":[{"name":"bob"},{"age":10}]})
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
> db.test2.find({"$or":[{"age":{"$in":[10,28]}},{"name":"bob"}]})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age"
mysql热搜
- CiscoACL在VLAN或端口上阻止蠕虫端口
- Oracle中EBS更新银行地址
- 2013年计算机等级考试二级Access第一章精讲4
- 等级考试二级Access考点分析:数据库和表
- Mac下MySql的启动、停止
- 在Ubuntu中安装MySQL
- 项目经理修炼之道(2)--必须读的书
- 微软8年潜行高性能计算发布08Beta2
- 操作系统之进程管理(2)
- 数据库中的表以行和列来组织数据,每一行称为每一列称为
- mysql存储过程中使用动态sql语句
- 怎么修改mysql的默认端口号
- mysql索引更新要多久
- sql语言容易学吗?
- MySQL中十进制转八进制的实现方法
- sql中查询命令的关键字是哪些?
- sql增删改查语句怎么写
- sql求和语句怎么写?
- SQL命令有哪几种类型
- mysqlworkbench是什么
- DBMS的体系结构有哪些类型
- 如何理解数据库事务隔离级别及脏读、不可重复读、幻读
- 使用JAVA进行数据库部分知识的操作代码
- 系统整理SQL中GroupBy的用法与GroupBy多个字段限制
- mysqlcount查询速度很慢怎么办?mysql查询速度优化方案