ให้เราใช้ไวยากรณ์ข้างต้นเพื่อค้นหาเอกสารทั้งหมดใน MongoDB ที่มีชื่อฟิลด์ “StudentFirstName” แบบสอบถามมีดังนี้ −
> db.getCollectionNames().forEach(function(myCollectionName) {
... var frequency = db[myCollectionName].find({"StudentFirstName": {$exists: true}}).count();
... if (frequency > 0) {
... print(myCollectionName);
... }
... }); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
multiDimensionalArrayProjection removeKeyFieldsDemo stringOrIntegerQueryDemo
ให้เราตรวจสอบคอลเลกชัน removeKeyFieldsDemo ว่ามีช่องที่มีชื่อ “StudentFirstName” หรือไม่ ต่อไปนี้เป็นแบบสอบถาม -
> db.removeKeyFieldsDemo.find({"StudentFirstName":{$exists:true}}); สิ่งนี้จะสร้างผลลัพธ์ที่แสดงฟิลด์ StudentFirstName ดังต่อไปนี้ -
{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" }
{ "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }