阅读 156

MongoDB 查询文档

在本章中,我们将学习如何从MongoDB集合中查询文档。

find()方法

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

语法

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

>db.COLLECTION_NAME.find()

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

在线示例

假设我们已经创建了一个名为 mycol 的集合-

> use sampleDBswitched to db sampleDB> db.createCollection("mycol"){ "ok" : 1 }>

并使用insert()方法在其中插入3个文档,如下所示-

> db.mycol.insert([
	{		title: "MongoDB Overview",		description: "MongoDB不是SQL数据库",		by: "基础教程",		url: "http://www.nhooo.com",		tags: ["mongodb", "database", "NoSQL"],		likes: 100
	},
	{		title: "NoSQL Database",		description: "NoSQL数据库没有表",		by: "基础教程",		url: "http://www.nhooo.com",		tags: ["mongodb", "database", "NoSQL"],		likes: 20,		comments: [
			{				user:"user1",				message: "My first comment",				dateCreated: new Date(2013,11,10,2,35),				like: 0
			}
		]
	}
])

以下方法检索集合中的所有文档-

> db.mycol.find()
{ "_id" : ObjectId("5dd4e2cc0821d3b44607534c"), "title" : "MongoDB Overview", "description" : "MongoDB不是SQL数据库", "by" : "基础教程", "url" : "http://www.nhooo.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5dd4e2cc0821d3b44607534d"), "title" : "NoSQL Database", "description" : "NoSQL数据库没有表", "by" : "基础教程", "url" : "http://www.nhooo.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-09T21:05:00Z"), "like" : 0 } ] }
>

pretty()方法

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

语法

>db.COLLECTION_NAME.find().pretty()

在线示例

以下示例从名为 mycol 的集合中检索所有文档,并以易于阅读的格式排列它们。

> db.mycol.find().pretty()
{	"_id" : ObjectId("5dd4e2cc0821d3b44607534c"),	"title" : "MongoDB Overview",	"description" : "MongoDB不是SQL数据库",	"by" : "基础教程",	"url" : "http://www.nhooo.com",	"tags" : [		"mongodb",		"database",		"NoSQL"
	],	"likes" : 100}
{	"_id" : ObjectId("5dd4e2cc0821d3b44607534d"),	"title" : "NoSQL Database",	"description" : "NoSQL数据库没有表",	"by" : "基础教程",	"url" : "http://www.nhooo.com",	"tags" : [		"mongodb",		"database",		"NoSQL"
	],	"likes" : 20,	"comments" : [
		{			"user" : "user1",			"message" : "My first comment",			"dateCreated" : ISODate("2013-12-09T21:05:00Z"),			"like" : 0
		}
	]
}

findOne()方法

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

语法

>db.COLLECTIONNAME.findOne()

在线示例

以下示例检索标题为MongoDB Overview的文档。

> db.mycol.findOne({title: "MongoDB Overview"})
{	"_id" : ObjectId("5dd6542170fb13eec3963bf0"),	"title" : "MongoDB Overview",	"description" : "MongoDB不是SQL数据库",	"by" : "基础教程",	"url" : "http://www.nhooo.com",	"tags" : [		"mongodb",		"database",		"NoSQL"
	],	"likes" : 100}

MongoDB 与 RDBMS Where 语句比较

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

操作格式范例RDBMS中的类似语句
等于{<key>:<value>}db.col.find({"by":"基础教程"}).pretty()where by = '基础教程'
小于{<key>:{$lt:<value>}}db.col.find({"likes":{$lt:50}}).pretty()where likes < 50
小于或等于{<key>:{$lte:<value>}}db.col.find({"likes":{$lte:50}}).pretty()where likes <= 50
大于{<key>:{$gt:<value>}}db.col.find({"likes":{$gt:50}}).pretty()where likes > 50
大于或等于{<key>:{$gte:<value>}}db.col.find({"likes":{$gte:50}}).pretty()where likes >= 50
不等于{<key>:{$ne:<value>}}db.col.find({"likes":{$ne:50}}).pretty()where likes != 50
值在数组中{<key>:{$in:[<value1>, <value2>,……<valueN>]}}db.mycol.find({"name":{$in:["Raj", "Ram", "Raghu"]}}).pretty()其中name与其中的任何值匹配 :["Raj", "Ram", "Raghu"]
值不在数组中{<key>:{$nin:<value>}}db.mycol.find({"name":{$nin:["Ramu", "Raghav"]}}).pretty()name 值不在数组中 :["Ramu", "Raghav"] 或者,根本不存在

MongoDB AND 条件

语法

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

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

示例

下面的实例将显示所有的教程由“ 基础教程”编写的教程和标题为“ MongoDB Overview”。

> db.mycol.find({$and:[{"by":"基础教程"},{"title": "MongoDB Overview"}]}).pretty()
{	"_id" : ObjectId("5dd4e2cc0821d3b44607534c"),	"title" : "MongoDB Overview",	"description" : "MongoDB不是SQL数据库",	"by" : "基础教程",	"url" : "https://www.nhooo.com",	"tags" : [		"mongodb",		"database",		"NoSQL"
	],	"likes" : 100}
>

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

MongoDB OR 条件

语法

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

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

示例

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

>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB不是SQL数据库",
   "by": "基础教程",
   "url": "http://www.nhooo.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"}
>

MongoDB AND 和 OR 一起使用

在线示例

下面的示例将显示点赞(likes)大于10且标题为“ MongoDB概述”或 by为“ 基础教程”的文档。等效于SQL where子句为 'where likes>10 AND (by = '基础教程' OR title = 'MongoDB概述')'

>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"},
   {"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB概述", 
   "description": "MongoDB不是SQL数据库",
   "by": "基础教程",
   "url": "http://www.nhooo.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"}
>

MongoDB NOR 条件

语法

要根据NOR条件查询文档,需要使用$nor关键字。以下是NOR的基本语法:

>db.COLLECTION_NAME.find(
	{
		$nor: [
			{key1: value1}, {key2:value2}
		]
	}
)

示例

假设我们在集合 empDetails 中插入了3个文档,如下所示-

db.empDetails.insertMany(
	[
		{			First_Name: "Radhika",			Last_Name: "Sharma",			Age: "26",			e_mail: "radhika_sharma.123@gmail.com",			phone: "9000012345"
		},
		{			First_Name: "Rachel",			Last_Name: "Christopher",			Age: "27",			e_mail: "Rachel_Christopher.123@gmail.com",			phone: "9000054321"
		},
		{			First_Name: "Fathima",			Last_Name: "Sheik",			Age: "24",			e_mail: "Fathima_Sheik.123@gmail.com",			phone: "9000054321"
		}
	]
)

以下示例将检索名字不是“ Radhika”且名字也不是“ Christopher”的文档

> db.empDetails.find(
	{
		$nor:[			40
			{"First_Name": "Radhika"},
			{"Last_Name": "Christopher"}
		]
	}
).pretty()
{	"_id" : ObjectId("5dd631f270fb13eec3963bef"),	"First_Name" : "Fathima",	"Last_Name" : "Sheik",	"Age" : "24",	"e_mail" : "Fathima_Sheik.123@gmail.com",	"phone" : "9000054321"}

MongoDB NOT 条件

语法

要根据NOT条件查询文档,您需要使用$not关键字,以下是NOT基本语法:

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

在线示例

以下示例将检索年龄不超过25岁的文档

> db.empDetails.find( { "Age": { $not: { $gt: "25" } } } )
{	"_id" : ObjectId("5dd6636870fb13eec3963bf7"),	"First_Name" : "Fathima",	"Last_Name" : "Sheik",	"Age" : "24",	"e_mail" : "Fathima_Sheik.123@gmail.com",	"phone" : "9000054321"}


文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐