经过前几天的学习之路, 今天终于到了重头戏了那就是文档查询操作话不多说哈, 直接看下语法:
db.collection.find(query, projection)
query : 可选, 使用查询操作符指定查询条件
projection : 可选, 使用投影操作符指定返回的键查询时返回文档中所有键值, 只需省略该参数即可 (默认省略)
这里还有一种读取的方式, 是格式化显示数据的, 看一下语法:
>db.luyaran.find().pretty()
好啦, 大家来看一下实例, 比较容易理解:
- > db.luyaran.find().pretty()
- {
- "_id" : ObjectId("56063f17ade2f21f36b03133"),
- "title" : "MongoDB 教程",
- "description" : "MongoDB 是一个 Nosql 数据库",
- "by" : "luyaran",
- "url" : "http://www.luyaran.com",
- "tags" : [
- "mongodb",
- "database",
- "NoSQL"
- ],
- "likes" : 100
- }
看到区别了么, 和昨天的查询相比较的话, 这种方式的查询展示数据格式比较清晰, 还有就是 findOne() 这种查询呢, 可以只展示我们查询出来的第一条数据接下来呢, 就轮到条件查询了, 首先我们要说的就是比较查询, 先来看一下和 mysql 中的比较符号的区别:
操作 | 格式 | 范例 | RDBMS 中的类似语句 |
---|---|---|---|
等于 | {<key>:<value>} | db.col.find({"by":"luyaran"}).pretty() | where by = 'luyaran' |
小于 | { | db.col.find({"likes":{$lt:50}}).pretty() | where likes < 50 |
小于或等于 | { | db.col.find({"likes":{$lte:50}}).pretty() | where likes <= 50 |
大于 | { | db.col.find({"likes":{$gt:50}}).pretty() | where likes > 50 |
大于或等于 | { | db.col.find({"likes":{$gte:50}}).pretty() | where likes >= 50 |
不等于 | { | db.col.find({"likes":{$ne:50}}).pretty() | where likes != 50 |
还有呢就是我们的 and 查询了, MongoDB 的 find() 查询可以传入多个键值, 以逗号隔开就是我们平常用的 and 多条件查询了大家来看一下语法:
>db.luyaran.find({key1:value1, key2:value2}).pretty()
下面的就是实例了:
- > db.luyaran.find({"by":"luyaran", "title":"MongoDB 教程"}).pretty()
- {
- "_id" : ObjectId("56063f17ade2f21f36b03133"),
- "title" : "MongoDB 教程",
- "description" : "MongoDB 是一个 Nosql 数据库",
- "by" : "luyaran",
- "url" : "http://www.luyaran.com",
- "tags" : [
- "mongodb",
- "database",
- "NoSQL"
- ],
- "likes" : 100
- } // 类似于 where by = 'luyaran' and title = 'MongoDB 教程'
再来就是我们的 or 查询喽, 看一下语法:
- >db.luyaran.find(
- {
- $or: [
- {key1: value1}, {key2:value2}
- ]
- }
- ).pretty()
看一下实例感受一下:
- > db.luyaran.find({
- $or: [{
- "by": "luyaran"
- },
- {
- "title": "MongoDB 教程"
- }]
- }).pretty() {
- "_id": ObjectId("56063f17ade2f21f36b03133"),
- "title": "MongoDB 教程",
- "description": "MongoDB 是一个 Nosql 数据库",
- "by": "luyaran",
- "url": "http://www.luyaran.com",
- "tags": ["mongodb", "database", "NoSQL"],
- "likes": 100
- }>
接下来看一下 or 和 and 的组合使用:
- >db.luyaran.find({"likes": {$gt:50}, $or: [{"by": "luyaran"},{"title": "MongoDB 教程"}]}).pretty()
- {
- "_id" : ObjectId("56063f17ade2f21f36b03133"),
- "title" : "MongoDB 教程",
- "description" : "MongoDB 是一个 Nosql 数据库",
- "by" : "luyaran",
- "url" : "http://www.luyaran.com",
- "tags" : [
- "mongodb",
- "database",
- "NoSQL"
- ],
- "likes" : 100
- }
到这里呢, 查询就差不多了, 接下来继续进入今天的重头戏条件操作符的使用不做过多的赘述了, 看实例哈:
首先, 我们先清除 luyaran 这个集合中的所有数据:
db.luyaran.remove({})
然后呢, 我们插入以下数据:
- >db.luyaran.insert({
- title: 'PHP 教程',
- description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言',
- by: 'luyaran',
- url: 'http://www.luyaran.com',
- tags: ['php'],
- likes: 200
- })
- >db.luyaran.insert({title: 'Java 教程',
- description: 'Java 是由 Sun Microsystems 公司于 1995 年 5 月推出的高级程序设计语言',
- by: 'luyaran',
- url: 'http://www.luyaran.com',
- tags: ['java'],
- likes: 150
- })
- >db.luyaran.insert({title: 'MongoDB 教程',
- description: 'MongoDB 是一个 Nosql 数据库',
- by: 'luyaran',
- url: 'http://www.luyaran.com',
- tags: ['mongodb'],
- likes: 100
- })
接下来我们使用 find 来查看一下当前集合中所含有的数据:
- > db.luyaran.find()
- { "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "php" ], "likes" : 200 }
- { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由 Sun Microsystems 公司于 1995 年 5 月推出的高级程序设计语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
- { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb" ], "likes" : 100 }
然后就开始查询喽, 先来一个 likes 大于 100 的:
db.luyaran.find({"likes" : {$gt : 100}}) // 类似于 select * from luyaran where likes> 100
输出的结果集为:
- > db.luyaran.find({"likes" : {$gt : 100}})
- { "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "php" ], "likes" : 200 }
- { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由 Sun Microsystems 公司于 1995 年 5 月推出的高级程序设计语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
- >
再来一个 likes 大于等于 100 的:
db.luyaran.find({likes : {$gte : 100}}) // 类似于 select * from luyaran where likes>= 100
输出的结果集为:
- > db.luyaran.find({likes : {$gte : 100}})
- { "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "php" ], "likes" : 200 }
- { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由 Sun Microsystems 公司于 1995 年 5 月推出的高级程序设计语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
- { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb" ], "likes" : 100 }
- >
然后呢, 再来一个小于 150 的:
db.luyaran.find({likes : {$lt : 150}}) // 类似于 select * from luyaran where likes <150
输出的结果集为:
- > db.luyaran.find({likes : {$lt : 150}})
- { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb" ], "likes" : 100 }
还有就是小于等于 150 的:
db.luyaran.find({likes : {$lte : 150}}) // 类似于 select * from luyaran where likes <= 150
输出的结果集为:
- > db.luyaran.find({likes : {$lte : 150}})
- { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由 Sun Microsystems 公司于 1995 年 5 月推出的高级程序设计语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
- { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb" ], "likes" : 100 }
最后再来一个区间的哈, 大于 100 小于 200 的:
db.luyaran.find({likes : {$lt :200, $gt : 100}}) // 类似于 select * from luyaran where likes> 100 and likes <200
输出的结果集为:
- > db.luyaran.find({likes : {$lt :200, $gt : 100}})
- { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由 Sun Microsystems 公司于 1995 年 5 月推出的高级程序设计语言", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "java" ], "likes" : 150 }
- >
好啦, 今天的分享到这里就差不多结束了后续的还有好多查询方式, 待我融会贯通后, 定会记录下来与诸君分享
另外, 再次声明哈, 这是本人学习笔记, 各位看官勿喷哈
如果觉得不错的, 欢迎诸位点赞支持哈
来源: https://blog.csdn.net/luyaran/article/details/79711560