ให้เราสร้างคอลเลกชันที่มีเอกสาร -
> 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" }