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"。

链接:https://www.learnfk.comhttps://www.learnfk.com/mongodb/mongodb-query-document.html

来源:LearnFk无涯教程网

>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"
}
>

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

技术教程推荐

邱岳的产品手记 -〔邱岳〕

Service Mesh实践指南 -〔周晶〕

说透中台 -〔王健〕

Netty源码剖析与实战 -〔傅健〕

流程型组织15讲 -〔蒋伟良〕

深入剖析Java新特性 -〔范学雷〕

李智慧 · 高并发架构实战课 -〔李智慧〕

B端产品经理入门课 -〔董小圣〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

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