在本章中,我们将学习如何从mongodb集合中查询文档。
要从mongodb集合查询数据,需要使用mongodb的find()
方法。
语法
find()
命令的基本语法如下:
>db.collection_name.find(document)
find()
方法将以非结构化的方式显示所有文档。
要以格式化的方式显示结果,可以使用pretty()
方法。
语法
> db.mycol.find().pretty()
示例
>db.mycol.find().pretty()
{
"_id": 100,
"title": "mongodb overview",
"description": "mongodb is no sql database",
"by": "h3 tutorials",
"url": "http://www.h3.com",
"tags": ["mongodb", "database", "nosql"],
"likes": "100"
}
>
除了find()
方法外,还有一个findone()
方法,它只返回一个文档。
要在一些条件的基础上查询文档,可以使用以下操作。
操作 | 语法 | 示例 | rdbms等效语句 |
---|---|---|---|
相等 | {<key>:<value>} |
db.mycol.find({"by":"h3"}).pretty() |
where by = 'h3' |
小于 | {<key>:{$lt:<value>}} |
db.mycol.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
小于等于 | {<key>:{$lte:<value>}} |
db.mycol.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
大于 | {<key>:{$gt:<value>}} |
db.mycol.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
大于等于 | {<key>:{$gte:<value>}} |
db.mycol.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
不等于 | {<key>:{$ne:<value>}} |
db.mycol.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
下面我们将对上表中的所有操作演示 -
语法
在find()
方法中,如果通过使用’,
‘将它们分开传递多个键,则 mongodb 将其视为and
条件。 以下是and
的基本语法 -
>db.mycol.find(
{
$and: [
{key1: value1}, {key2:value2}
]
}
).pretty()
示例
以下示例将显示由“h3 tutorials
”编写并且标题为“mongodb overview”的所有教程。
> db.mycol.find({$and:[{"by":"h3 tutorials"},{"title": "mongodb overview"}]}).pretty()
{
"_id" : 100,
"title" : "mongodb overview",
"description" : "mongodb is no sql database",
"by" : "h3 tutorials",
"url" : "http://www.h3.com",
"tags" : [
"mongodb",
"database",
"nosql"
],
"likes" : 100
}
>
对于上面给出的例子,等效的sql where
子句是 -
select * from mycol where by ='h3 tutorials' and title ='mongodb overview'
可以在find
子句中传递任意数量的键值。
语法
在要根据or
条件查询文档,需要使用$or
关键字。 以下是or
条件的基本语法 -
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
示例
以下示例将显示由“h3 tutorials
”编写或标题为“mongodb overview”的所有教程。
>db.mycol.find({$or:[{"by":"h3 tutorials"},{"title": "mongodb overview"}]}).pretty()
{
"_id": 100,
"title": "mongodb overview",
"description": "mongodb is no sql database",
"by": "h3 tutorials",
"url": "http://www.h3.com",
"tags": ["mongodb", "database", "nosql"],
"likes": "100"
}
>
示例
以下示例将显示likes
大于10
以及标题是“mongodb overview
”或者“h3 tutorials
”的所有文档。 等价sql where子句为 -
select * from mycol where likes> 10 and(by ='h3 tutorials' or title ='mongodb overview')
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "h3 tutorials"},
{"title": "mongodb overview"}]}).pretty()
{
"_id": 100,
"title": "mongodb overview",
"description": "mongodb is no sql database",
"by": "h3 tutorials",
"url": "http://www.h3.com",
"tags": ["mongodb", "database", "nosql"],
"likes": "100"
}
>
这里演示如何使用:db.collection.find()
方法对嵌入/嵌套文档的查询操作的示例。 此页面上的示例使用inventory
集合。要填充库存(inventory
)集合以准备一些数据,请运行以下命令:
db.inventory.insertmany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "a" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "a" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "d" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "d" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "a" }
]);
匹配嵌入/嵌套文档
要在作为嵌入/嵌套文档的字段上指定相等条件,请使用查询过滤器文档{<field>:<value>}
,其中<value>
是要匹配的文档。
例如,以下查询选择字段size
等于{ h: 14, w: 21, uom: "cm" }
的所有文档:
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
整个嵌入式文档中的相等匹配需要精确匹配指定的<value>
文档,包括字段顺序。
例如,以下查询与库存(inventory
)集合中的任何文档不匹配:
db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
查询嵌套字段
要在嵌入/嵌套文档中的字段上指定查询条件,请使用点符号(“field.nestedfield
”)。
在嵌套字段上指定等于匹配
以下示例选择在size
字段中嵌套的字段uom
等于“in
”的所有文档:
db.inventory.find( { "size.uom": "in" } )
使用查询运算符指定匹配
查询过滤器文档可以使用查询运算符来指定,如以下形式的条件:
{ <field1>: { <operator1>: <value1> }, ... }
以下查询使用size
字段中嵌入的字段h
中的小于运算符($lt
):
db.inventory.find( { "size.h": { $lt: 15 } } )
指定and条件
以下查询选择嵌套字段h
小于15
的所有文档,嵌套字段uom
等于“in
”,status
字段等于“d
”:
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "d" } )