ให้เราสร้างคอลเลกชันที่มีเอกสาร -
> db.demo711.insertOne({Name:"John","Marks":75,Age:21,status:"Active"});{ "acknowledged" :true, "insertedId" :ObjectId("5ea85c215d33e20ed1097b7e ")}> db.demo711.insertOne({Name:"Chris","Marks":55,Age:22,status:"Active"});{ "acknowledged" :true, "insertedId" :ObjectId("5ea85c2c5d33e20ed1097b7f ")}> db.demo711.insertOne({ชื่อ:"Bob","Marks":45,Age:20,status:"Inactive"});{ "acknowledged" :true, "insertedId" :ObjectId("5ea85c3e5d33e20ed1097b80 ")}> db.demo711.insertOne({ชื่อ:"David","Marks":85,Age:23,status:"Active"});{ "acknowledged" :true, "insertedId" :ObjectId("5ea85c775d33e20ed1097b81 ")}
แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -
> db.demo711.find();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" :ObjectId("5ea85c215d33e20ed1097b7e"), "Name" :"John", "Marks" :75, "Age" :21, "status" :"Active" }{ "_id" :ObjectId( "5ea85c2c5d33e20ed1097b7f"), "Name" :"Chris", "Marks" :55, "Age" :22, "status" :"Active" }{ "_id" :ObjectId("5ea85c3e5d33e20ed1097b80"), "Name" :" Bob", "Marks" :45, "Age" :20, "status" :"Inactive" }{ "_id" :ObjectId("5ea85c775d33e20ed1097b81"), "Name" :"David", "Marks" :85, " อายุ" :23, "สถานะ" :"ใช้งานอยู่" }
ต่อไปนี้เป็นแบบสอบถาม MongoDB ด้วย $or −
> db.demo711.find( { $or:[ { Age:{$gte:23}}, {Marks:{$gt:80} }, {status:'Inactive'} ] });ก่อน>สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" :ObjectId("5ea85c3e5d33e20ed1097b80"), "Name" :"Bob", "Marks" :45, "Age" :20, "status" :"Inactive" }{ "_id" :ObjectId( "5ea85c775d33e20ed1097b81"), "Name" :"David", "Marks" :85, "Age" :23, "status" :"Active" }