(一)查询文档

查询文档可以使用以下方法

# 以非结构化的方式显示所有的文档
db.<collectionName>.find(document) # 以结构化的方式显示所有文档
db.<collectionName>.find(document).pretty() # 只返回一个文档(结构化方式)
db.<collectionName>.findOne()

测试1 : 使用find()方法以非结构化的方式查询文档

> db.blog.find()
{ "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"), "title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理", "Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html", "summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...", "tags" : [ "Linux", "study" ], "post" : "2020-05-13 23:17", "views" : 57, "comments" : [ { "user" : "user1", "message" : "mark!", "like" : 0 } ] }
{ "_id" : ObjectId("5ebd71b4c50e24a9d8fb2a7b"), "title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理", "Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html", "summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...", "tags" : [ "Linux", "study" ], "post" : "2020-05-13 23:17", "views" : 57, "comments" : [ { "user" : "user1", "message" : "mark!", "like" : 0 } ] }
{ "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"), "title" : "如何为Linux服务器添加磁盘", "Link" : "https://www.cnblogs.com/lijiaman/p/12885028.html", "summary" : "Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...", "tags" : [ "Linux", "study" ], "post" : "2020-05-13 21:31", "views" : 25, "comments" : "" }
{ "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7d"), "title" : "MySQL闪回工具--MyFlash", "Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html", "summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...", "tags" : [ "mysql", "study" ], "post" : "2020-04-24 21:38", "views" : 23, "comments" : "" }
>

测试2:使用pretty()方法以结构化的方式查询文档

> db.blog.find().pretty()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
{
"_id" : ObjectId("5ebd71b4c50e24a9d8fb2a7b"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
{
"_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
"title" : "如何为Linux服务器添加磁盘",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885028.html",
"summary" : "Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 21:31",
"views" : 25,
"comments" : ""
}
{
"_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7d"),
"title" : "MySQL闪回工具--MyFlash",
"Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html",
"summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
"tags" : [
"mysql",
"study"
],
"post" : "2020-04-24 21:38",
"views" : 23,
"comments" : ""
}
>

测试3: 使用findOne()方法返回一个结构化文档

> db.blog.findOne()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
>

(二)MongoDB与RDBMS等效的where子句


操作 语法 例子 RDBMS等效例子
相等(=) {<key>:<value>} db.blog.find({title:"MySQL闪回工具--MyFlash"}) where title="MySQL闪回工具--MyFlash"
大于(>) {<key>:{$gt:<value>}} db.blog.find({views:{$gt:40}}) where views > 40
大于等于(>=) {<key>:{$gte:<value>}} db.blog.find({views:{$gte:57}}) where views>=57
小于(<) {<key>:{$lt:<value>}} db.blog.find({views:{$lt:25}}) where views<25
小于等于(<=) {<key>:{$lte:<value>}} db.blog.find({views:{$lte:25}}) where views<=25
不等于(<>) {<key>:{$ne:<value>}} db.blog.find({views:{$ne:25}}) where views!=25

例子1:查看blog集合中标题为“MySQL闪回工具--MyFlash”的文档

> db.blog.find({title:"MySQL闪回工具--MyFlash"}).pretty()
{
"_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7d"),
"title" : "MySQL闪回工具--MyFlash",
"Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html",
"summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
"tags" : [
"mysql",
"study"
],
"post" : "2020-04-24 21:38",
"views" : 23,
"comments" : ""
}
>

例子2 :查看blog集合中浏览次数大于40的文档

> db.blog.find({views:{$gt:40}}).pretty()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
>

例子3 :查看blog集合中浏览次数大于等于57次的文档

> db.blog.find({views:{$gte:57}}).pretty()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
>

例子4:查询blog集合中浏览次数小于25次的文档

> db.blog.find({views:{$lt:25}}).pretty()
{
"_id" : ObjectId("5ebe674278420c5d36520584"),
"title" : "MySQL闪回工具--MyFlash",
"Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html",
"summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
"tags" : [
"mysql",
"study"
],
"post" : "2020-04-24 21:38",
"views" : 23,
"comments" : ""
}
>

例子5:查询blog集合中浏览此时小于等于25次的文档

> db.blog.find({views:{$lte:25}}).pretty()
{
"_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
"title" : "如何为Linux服务器添加磁盘",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885028.html",
"summary" : "Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 21:31",
"views" : 25,
"comments" : ""
}
{
"_id" : ObjectId("5ebe674278420c5d36520584"),
"title" : "MySQL闪回工具--MyFlash",
"Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html",
"summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
"tags" : [
"mysql",
"study"
],
"post" : "2020-04-24 21:38",
"views" : 23,
"comments" : ""
}
>

例子6:查询blog集合中浏览次数不等于25次的文档

> db.blog.find({views:{$ne:25}}).pretty()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
{
"_id" : ObjectId("5ebe674278420c5d36520584"),
"title" : "MySQL闪回工具--MyFlash",
"Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html",
"summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
"tags" : [
"mysql",
"study"
],
"post" : "2020-04-24 21:38",
"views" : 23,
"comments" : ""
}
>

(三)MongoDB中的AND操作

AND的语法:

db.<collectionName>.find(
{
$and:[
{key1:value1},{key2:value2}
]
}
).pretty()

例子 :查询blog集合中标题为“如何为Linux服务器添加磁盘”并且浏览次数大于20次的文档

> db.blog.find(
... {
... $and : [
... {title:"如何为Linux服务器添加磁盘"},
... {views:{$gt:20}}
... ]
... }
... ).pretty() {
"_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
"title" : "如何为Linux服务器添加磁盘",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885028.html",
"summary" : "Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 21:31",
"views" : 25,
"comments" : ""
}
>

上面的查询相当于RDBMS中的:

select * from blog where title="如何为Linux服务器添加磁盘" and views>20;

(四)MongoDB中的or操作

OR的语法:

db.<collectionName>.find(
{
$or:[
{<key1>:<value1>},{<key2>:<value2>}
]
}
).pretty()

例子:查询blog集合中访问量在40以上或者是标题为“MySQL闪回工具--MyFlash”的文档

> db.blog.find(
... {
... $or : [
... {views:{$gt:40}},
... {title:"MySQL闪回工具--MyFlash"}
... ]
... }
... ).pretty() {
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
{
"_id" : ObjectId("5ebe674278420c5d36520584"),
"title" : "MySQL闪回工具--MyFlash",
"Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html",
"summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
"tags" : [
"mysql",
"study"
],
"post" : "2020-04-24 21:38",
"views" : 23,
"comments" : ""
}
>

上面的查询相当于RDBMS中的:

select * from blog where views>40 or title="MySQL闪回工具--MyFlash"

(五)MongoDB中的AND和OR结合在一起

例子:查看bolg集合中Link为"https://www.cnblogs.com/lijiaman/p/12770415.html"且浏览量大于40或者是标题为"MySQL闪回工具--MyFlash"的文档。

> db.blog.find(
... {
... Link : "https://www.cnblogs.com/lijiaman/p/12770415.html",
... $or : [{views:{$gt:40}},{title:"MySQL闪回工具--MyFlash"}]
... }
... ).pretty()
{
"_id" : ObjectId("5ebe674278420c5d36520584"),
"title" : "MySQL闪回工具--MyFlash",
"Link" : "https://www.cnblogs.com/lijiaman/p/12770415.html",
"summary" : "MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
"tags" : [
"mysql",
"study"
],
"post" : "2020-04-24 21:38",
"views" : 23,
"comments" : ""
}
>

上面的查询相当于RDBMS中的:

select *
from blog
where link='https://www.cnblogs.com/lijiaman/p/12770415.html'
and (views>40 or title='MySQL闪回工具--MyFlash')

(六)MongoDB中的嵌套查询

对于文档里面还包含文档的情况,可以使用嵌套查询,查询内部文档信息。

(6.1)匹配嵌套文档


例子:查询blog集合中comments字段等于{ "user" : "user1", "message" : "mark!", "like" : 0 }的文档

> db.blog.find({comments:{ "user" : "user1", "message" : "mark!", "like" : 0 }}).pretty()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
>

(6.2)查询嵌套字段

父字段与子字段之间用“.”隔开

例子:查询blog表中comments字段中嵌套的字段user等于“user1”的文档

> db.blog.find({"comments.user":"user1"}).pretty()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}
>

(6.3)嵌套查询指定AND条件

查询comments字段中user字段为“user1”,comments字段中like字段为0,views字段为57的文档。

> db.blog.find({"comments.user":"user1" , "comments.like":0,views:57}).pretty()
{
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}

或者可以直接使用$and来查询

> db.blog.find({
... $and : [
... {"comments.user":"user1"},
... {"comments.like":0},
... {views:57}
... ]
... }).pretty() {
"_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
"title" : "Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
"Link" : "https://www.cnblogs.com/lijiaman/p/12885649.html",
"summary" : "(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
"tags" : [
"Linux",
"study"
],
"post" : "2020-05-13 23:17",
"views" : 57,
"comments" : [
{
"user" : "user1",
"message" : "mark!",
"like" : 0
}
]
}

【完】

MongoDB文档(二)--查询的更多相关文章

  1. MongoDB 文档的查询和插入操作

    MongoDB是文档型数据库,有一些专门的术语,和关系型DB相似,但也有差异,例如,Collection类似于关系型DB的Table,document类似于row,key/value pair类似于c ...

  2. MongoDB的学习--文档的查询

    继续关于<MongoDB权威指南>记录,今天的内容是文档的查询~~ MongoDB官网地址:http://www.mongodb.org/ 我使用的是MongoDB 2.4.8 find函 ...

  3. MongoDB(四):数据类型、插入文档、查询文档

    1. 数据类型 MongoDB支持许多数据类型. 字符串 - 这是用于存储数据的最常用的数据类型.MongoDB中的字符串必须为UTF-8. 整型 - 此类型用于存储数值. 整数可以是32位或64位, ...

  4. mongodb内嵌文档的查询

    本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...

  5. MongoDB文档的基本操作

    1. MongoDB的安装方法 (1)下载MongoDB 相应的版本: (2)设置数据文件和日志文件的存放目录: (3)启动MongoDB服务: (4)将MongoDB作为服务启动. 2. Mongo ...

  6. MongoDB自学------(3)MongoDB文档操作

    一.插入文档 二.查询文档 三.更新文档 可以看到标题(title)由原来的 "Mongodb" 更新为了 "MongoDBtest". 以上语句只会修改第一条 ...

  7. mongodb文档的CRUD

    本章会介绍对数据库移入或者移出数据的基本操作 向集合添加文档 从集合删除文档 更新现有的文档 为这些操作选择合适的安全级别 添加删除数据库 添加数据库 :use foo  如果存在foo 就use   ...

  8. mongoDB 文档概念

    mongoDB 文档概念 什么是文档 文档是 mongodb 基本的数据组织单元,类似于mysql 中的记录 文档由多个键值对组成,每个键值对表达一个数据项 属于 bson 数据 ps:  bson ...

  9. mongoDB 文档操作_删

    mongoDB 文档删除 MySQL对比 mysql delete from table where ... mongo db.collection.deleteOne(query) 删除函数 del ...

随机推荐

  1. C#判断网址的可访问性

    /// <summary> /// 判断网址是否可以访问 /// </summary> /// <param name="Url"></p ...

  2. Kivy中显示汉字的问题

    1. kivy中显示中文乱码和提示错误的原因: 编码问题 字体问题 2. 字体问题的解决 可以下载支持中文的字体文件ttf,我这里使用了微软雅黑中文简体msyh.ttf.我们在编写布局时可以直接在相关 ...

  3. Java的字节流,字符流和缓冲流对比探究

    目录 一.前言 二.字节操作和字符操作 三.两种方式的效率测试 3.1 测试代码 3.2 测试结果 3.3 结果分析 四.字节顺序endian 五.综合对比 六.总结 一.前言 所谓IO,也就是Inp ...

  4. [Objective-C] 009_Foundation框架之NSDictionary与NSMutableDictionary

    在Cocoa Foundation中NSDictionary和NSMutableDictionary 用于对象有序集合,NSDictionary和NSMutableDictionary 与 NSArr ...

  5. python 验证码处理

    一. 灰度处理,就是把彩色的验证码图片转为灰色的图片. 二值化,是将图片处理为只有黑白两色的图片,利于后面的图像处理和识别 # 自适应阀值二值化 def _get_dynamic_binary_ima ...

  6. 设计Person类 代码参考

    #include <iostream> using namespace std; class Trapezium { private: int x1,y1,x2,y2,x3,y3,x4,y ...

  7. 使用PRTG和panabit结合定位网络阻塞的来源

    一.背景   在网络管理工作中,有时会出现网络阻塞,需要定位阻塞来源以采取措施解决问题.二.以一个网络阻塞案例说明定位方法   案例:某企业日常使用多条网络线路,某一段时间发现某条线路传输速率下降,对 ...

  8. jchdl - RTL Module

    https://mp.weixin.qq.com/s/Sr4ffU4TPPoUJpdInwWd6w ​​ jchdl Module类在概念上对应Verilog的module,作为所有用户自定义模块的父 ...

  9. Java实现 LeetCode 554 砖墙(缝隙可以放在数组?)

    554. 砖墙 你的面前有一堵方形的.由多行砖块组成的砖墙. 这些砖块高度相同但是宽度不同.你现在要画一条自顶向下的.穿过最少砖块的垂线. 砖墙由行的列表表示. 每一行都是一个代表从左至右每块砖的宽度 ...

  10. Java实现 蓝桥杯VIP 算法训练 集合运算

    问题描述 给出两个整数集合A.B,求出他们的交集.并集以及B在A中的余集. 输入格式 第一行为一个整数n,表示集合A中的元素个数. 第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素. 第三行 ...