MongoDB - 查询数据

MongoDB - 查询数据 首页 / MongoDB入门教程 / MongoDB - 查询数据

在本章中,无涯教程将学习如何从MongoDB集合中查询文档。

find()方法

要查询MongoDB集合中的数据,您需要使用MongoDB的 find()方法。

find()方法的基本语法如下-

>db.COLLECTION_NAME.find()

find()方法将以非结构化方式显示所有文档。

pretty()方法

要以格式化的方式显示输出,可以使用 pretty()方法。

无涯教程网

>db.mycol.find().pretty()

pretty示例

>db.mycol.find().pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "Learnfk point",
   "url": "http://www.learnfk.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

除了find()方法之外,还有 findOne()方法,该方法仅返回一个文档。

Equivalents查询

要根据某些条件查询文档,可以使用以下操作。

OperationSyntaxExampleRDBMS Equivalent
Equalit(等于){<key>:<value>}db.mycol.find({"by":"Learnfk point"}).pretty()where by='Learnfk point'
Less Than(小于){<key>:{$lt:<value>}}db.mycol.find({"likes":{$lt:50}}).pretty()where likes < 50
Less Than Equals(小于或等于){<key>:{$lte:<value>}}db.mycol.find({"likes":{$lte:50}}).pretty()where likes <= 50
Greater Than(大于){<key>:{$gt:<value>}}db.mycol.find({"likes":{$gt:50}}).pretty()where likes > 50
Greater Than Equals(大于或等于){<key>:{$gte:<value>}}db.mycol.find({"likes":{$gte:50}}).pretty()where likes >= 50
Not Equals(不等于){<key>:{$ne:<value>}}db.mycol.find({"likes":{$ne:50}}).pretty()where likes != 50

AND语句

在 find()方法中,如果通过用'分隔开多个键来传递多个键,则MongoDB会将其视为 AND 条件,以下是 AND 的基本语法-

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

以下示例将显示" Learnfk point"编写的所有教程,其标题为" MongoDB Overview"。

>db.mycol.find({$and:[{"by":"Learnfk point"},{"title": "MongoDB Overview"}]}).pretty() {
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "Learnfk point",
   "url": "http://www.learnfk.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}

对于上面给出的示例,等效的where子句将为',其中by ='Learnfk point'AND title ='MongoDB Overview'',您可以在find子句中传递任意数量的键,值对。

OR语句

要基于"$or"条件查询文档,您需要使用 $or 关键字。以下是 OR 的基本语法-

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

以下示例将显示所有由" Learnfk point"编写或标题为" MongoDB Overview"的教程。

>db.mycol.find({$or:[{"by":"Learnfk point"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "Learnfk point",
   "url": "http://www.learnfk.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

AND&OR示例

以下示例将显示点赞大于10且标题为“ MongoDB Overview”或by为“ Learnfk point”的文档。等价的SQL where子句为'where likes> 10 AND(by ='Learnfk point'OR title ='MongoDB Overview')

>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "Learnfk point"},
   {"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "Learnfk point",
   "url": "http://www.learnfk.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言从入门到实战 -〔蔡超〕

编译原理之美 -〔宫文学〕

DDD实战课 -〔欧创新〕

小马哥讲Spring核心编程思想 -〔小马哥〕

SRE实战手册 -〔赵成〕

软件设计之美 -〔郑晔〕

WebAssembly入门课 -〔于航〕

技术领导力实战笔记 2022 -〔TGO 鲲鹏会〕

零基础GPT应用入门课 -〔林健(键盘)〕

好记忆不如烂笔头。留下您的足迹吧 :)