让我们创建一个包含文档的集合-
> db.demo362.insertOne({"ClientName":"John","ClientProject":"School Management System"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a77454a481fef8ec7a1c") } > db.demo362.insertOne({"ClientName":"David","ClientProject":"Library Management System"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a78454a481fef8ec7a1d") } > db.demo362.insertOne({"ClientName":"Mike","ClientProject":"Event Tracker"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a7a054a481fef8ec7a1e") } > db.demo362.insertOne({"ClientName":"Carol","ClientProject":"Hospital Management System"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a7b754a481fef8ec7a1f") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo362.find();
这将产生以下输出-
{ "_id" : ObjectId("5e56a77454a481fef8ec7a1c"), "ClientName" : "John", "ClientProject" : "School Management System" } { "_id" : ObjectId("5e56a78454a481fef8ec7a1d"), "ClientName" : "David", "ClientProject" : "Library Management System" } { "_id" : ObjectId("5e56a7a054a481fef8ec7a1e"), "ClientName" : "Mike", "ClientProject" : "Event Tracker" } { "_id" : ObjectId("5e56a7b754a481fef8ec7a1f"), "ClientName" : "Carol", "ClientProject" : "Hospital Management System" }
以下是获取多个文档的查询-
> db.demo362.find({$or: [{"ClientName":"John"}, {"ClientProject": "Event Tracker" }]});
这将产生以下输出-
{ "_id" : ObjectId("5e56a77454a481fef8ec7a1c"), "ClientName" : "John", "ClientProject" : "School Management System" } { "_id" : ObjectId("5e56a7a054a481fef8ec7a1e"), "ClientName" : "Mike", "ClientProject" : "Event Tracker" }