Skip to content

Latest commit

 

History

History
executable file
·
42 lines (36 loc) · 1.36 KB

File metadata and controls

executable file
·
42 lines (36 loc) · 1.36 KB

4.5.3 查询文档

find()

要想查询 MongoDB 集合中的数据,使用 find() 方法,会以非结构化的方式来显示所有文档。

pretty()

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

插入以后,我们可以用“使用 find 函数查看数据库中的文档,也可以用pretty函数使查询结果的打印格式更加优雅。

> db.swiftdb.find().pretty()

{ "_id" : ObjectId("580ebd569ab3b93e870f44fd"), "name" : "lovemo" }
{ "_id" : ObjectId("580ec0429ab3b93e870f44ff") }
{ "_id" : ObjectId("580ec0479ab3b93e870f4500") }
{ "_id" : ObjectId("580ec04b9ab3b93e870f4501") }
{ "_id" : ObjectId("580ec04d9ab3b93e870f4502") }
{ "_id" : ObjectId("580ec0579ab3b93e870f4503") }
{ "_id" : ObjectId("580ec0659ab3b93e870f4504") }
{ "_id" : ObjectId("580ec0699ab3b93e870f4505") }
{ "_id" : ObjectId("580ec06b9ab3b93e870f4506") }
{ "_id" : ObjectId("580ec06d9ab3b93e870f4507") }
{
     "_id" : ObjectId("580ec2d69ab3b93e870f4508"),
     "name" : "lovemo",
     "tags" : "iOS",
     "url" : "https://github.com/lovemo"
}

在 find() 方法中,如果传入多个键,既可以使用条件查询

> db.swiftdb.find({"name": "lovemo"}).pretty()

{ "_id" : ObjectId("580ebd569ab3b93e870f44fd"), "name" : "lovemo" }
{
	"_id" : ObjectId("580ec2d69ab3b93e870f4508"),
	"name" : "lovemo",
	"tags" : "iOS",
	"url" : "https://github.com/lovemo"
}