ในการเลือกคอลัมน์ที่เฉพาะเจาะจง คุณสามารถละเว้นคอลัมน์ที่เหลือได้ เช่น เพื่อซ่อนคอลัมน์เหล่านั้น ให้ตั้งค่าเป็น 0 ขั้นแรกให้เราสร้างคอลเลกชันที่มีเอกสาร -
> db.demo415.insertOne({"ClientName":"Robert","ClientCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e72329db912067e57771adc")
}
> db.demo415.insertOne({"ClientName":"David","ClientCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e7232acb912067e57771add")
}
> db.demo415.insertOne({"ClientName":"Bob","ClientCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e7232b4b912067e57771ade")
} แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -
> db.demo415.find();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" : ObjectId("5e72329db912067e57771adc"), "ClientName" : "Robert", "ClientCountryName" : "US" }
{ "_id" : ObjectId("5e7232acb912067e57771add"), "ClientName" : "David", "ClientCountryName" : "UK" }
{ "_id" : ObjectId("5e7232b4b912067e57771ade"), "ClientName" : "Bob", "ClientCountryName" : "AUS" } ต่อไปนี้เป็นแบบสอบถามเพื่อเลือกคอลัมน์เฉพาะ ที่นี่ เราได้ละเว้นคอลัมน์ที่เหลือเพื่อแสดงคอลัมน์ “ClientCountryName” –
> db.demo415.find({},{_id:0,ClientName:0}); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "ClientCountryName" : "US" }
{ "ClientCountryName" : "UK" }
{ "ClientCountryName" : "AUS" }