Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

วิธีค้นหาเอกสารที่มีฟิลด์เฉพาะใน MongoDB (ไม่สนค่า)

การค้นหาเอกสารที่มีฟิลด์เฉพาะใน MongoDB (ไม่สนใจค่าของฟิลด์)

ใช้ ออเปอเรเตอร์ $exists เพื่อตรวจสอบว่าฟิลด์มีอยู่ในเอกสารหรือไม่ โดยไม่สนใจว่าค่าของฟิลด์เป็นอะไร (รวมถึง null, ค่าว่าง, หรือค่าใดๆ ก็ตาม)

ไวยากรณ์

db.yourCollectionName.find({yourFieldName: {$exists: true}});

ตัวอย่างการสร้างคอลเลกชันและเพิ่มข้อมูล

> db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"John","StudentAge":null});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d1d60a629b87623db1b22") }
> db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Larry","StudentAge":null});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d1d70a629b87623db1b23") }
> db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Chris","StudentAge":""});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d1d7ba629b87623db1b24") }
> db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Robert","StudentAge":""});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d1d81a629b87623db1b25") }

แสดงข้อมูลทั้งหมด

> db.findAllDocumentWhichHaveFieldDemo.find().pretty();

ผลลัพธ์:

{ "_id" : ObjectId("5c9d1d60a629b87623db1b22"), "StudentName" : "John", "StudentAge" : null }
{ "_id" : ObjectId("5c9d1d70a629b87623db1b23"), "StudentName" : "Larry", "StudentAge" : null }
{ "_id" : ObjectId("5c9d1d7ba629b87623db1b24"), "StudentName" : "Chris", "StudentAge" : "" }
{ "_id" : ObjectId("5c9d1d81a629b87623db1b25"), "StudentName" : "Robert", "StudentAge" : "" }

ค้นหาเอกสารที่มีฟิลด์ StudentAge (ไม่สนค่า)

> db.findAllDocumentWhichHaveFieldDemo.find({StudentAge: {$exists: true}});

ผลลัพธ์ (พบทั้ง 4 เอกสาร):

{ "_id" : ObjectId("5c9d1d60a629b87623db1b22"), "StudentName" : "John", "StudentAge" : null }
{ "_id" : ObjectId("5c9d1d70a629b87623db1b23"), "StudentName" : "Larry", "StudentAge" : null }
{ "_id" : ObjectId("5c9d1d7ba629b87623db1b24"), "StudentName" : "Chris", "StudentAge" : "" }
{ "_id" : ObjectId("5c9d1d81a629b87623db1b25"), "StudentName" : "Robert", "StudentAge" : "" }

ค้นหาและ Exclude ฟิลด์ StudentName

> db.findAllDocumentWhichHaveFieldDemo.find(
    {StudentAge: {$exists: true}}, 
    {StudentName: 0, StudentAge: 1}
);

ผลลัพธ์ (มีเฉพาะ _id และ StudentAge):

{ "_id" : ObjectId("5c9d1d60a629b87623db1b22"), "StudentAge" : null }
{ "_id" : ObjectId("5c9d1d70a629b87623db1b23"), "StudentAge" : null }
{ "_id" : ObjectId("5c9d1d7ba629b87623db1b24"), "StudentAge" : "" }
{ "_id" : ObjectId("5c9d1d81a629b87623db1b25"), "StudentAge" : "" }

คำอธิบาย $exists

  • {$exists: true} : ฟิลด์มีอยู่ในเอกสาร (match ทุกค่า รวมถึง null, "", 0, false, [], {})
  • {$exists: false} : ฟิลด์ไม่มีอยู่ในเอกสาร (missing field)

ความแตกต่างระหว่าง $exists และการเปรียบเทียบค่า

Querynull""0falseMissing
{field: {$exists: true}}✅ Match✅ Match✅ Match✅ Match❌ No Match
{field: null}✅ Match❌❌❌✅ Match*
{field: ""}❌✅ Match❌❌❌
{field: {$ne: null}}❌✅ Match✅ Match✅ Match✅ Match*

* MongoDB ถือว่า Missing field = null สำหรับการเปรียบเทียบบางแบบ

การใช้งานร่วมกับ $elemMatch สำหรับ Array

// ค้นหาเอกสารที่ array field มี element ที่ match เงื่อนไข
db.collection.find({ 
    tags: { $elemMatch: { $exists: true } } 
});

// ค้นหาเอกสารที่มี array field (ไม่ว่าจะว่างหรือไม่)
db.collection.find({ 
    tags: { $exists: true } 
});

กรณี Nested Field (Dot Notation)

// ตรวจสอบ nested field
db.collection.find({ "address.city": { $exists: true } });

// ตรวจสอบว่า object field มีอยู่
db.collection.find({ "address": { $exists: true } });

$exists เป็นเครื่องมือพื้นฐานสำคัญในการ validate schema และทำ data quality checks