Query สำหรับค่าที่ไม่เป็น Null และไม่ใช่ค่าว่างใน MongoDB
ใช้ ออเปอเรเตอร์ $nin (not in) เพื่อค้นหาเอกสารที่ค่าของฟิลด์ไม่ใช่ null และไม่ใช่สตริงว่าง ("")
ไวยากรณ์
db.yourCollectionName.find({yourFieldName: {$nin: [null, ""]}});
ตัวอย่างการสร้างคอลเลกชันและเพิ่มข้อมูล
> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Larry","UserAge":24});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d20b6a629b87623db1b26") }
> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"","UserAge":29});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d20bea629b87623db1b27") }
> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Sam","UserAge":32});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d20c7a629b87623db1b28") }
> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":null,"UserAge":27});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d20d2a629b87623db1b29") }
> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Robert","UserAge":26});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d20dda629b87623db1b2a") }
> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"","UserAge":23});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9d20e7a629b87623db1b2b") }
แสดงข้อมูลทั้งหมด
> db.notEqualToNullOrEmptyDemo.find().pretty();
ผลลัพธ์:
{ "_id" : ObjectId("5c9d20b6a629b87623db1b26"), "UserName" : "Larry", "UserAge" : 24 }
{ "_id" : ObjectId("5c9d20bea629b87623db1b27"), "UserName" : "", "UserAge" : 29 }
{ "_id" : ObjectId("5c9d20c7a629b87623db1b28"), "UserName" : "Sam", "UserAge" : 32 }
{ "_id" : ObjectId("5c9d20d2a629b87623db1b29"), "UserName" : null, "UserAge" : 27 }
{ "_id" : ObjectId("5c9d20dda629b87623db1b2a"), "UserName" : "Robert", "UserAge" : 26 }
{ "_id" : ObjectId("5c9d20e7a629b87623db1b2b"), "UserName" : "", "UserAge" : 23 }
ค้นหาเอกสารที่ UserName ไม่เป็น null และไม่ใช่ค่าว่าง
> db.notEqualToNullOrEmptyDemo.find({UserName: {$nin: [null, ""]}}).pretty();
ผลลัพธ์ (เหลือ Larry, Sam, Robert):
{ "_id" : ObjectId("5c9d20b6a629b87623db1b26"), "UserName" : "Larry", "UserAge" : 24 }
{ "_id" : ObjectId("5c9d20c7a629b87623db1b28"), "UserName" : "Sam", "UserAge" : 32 }
{ "_id" : ObjectId("5c9d20dda629b87623db1b2a"), "UserName" : "Robert", "UserAge" : 26 }
คำอธิบาย $nin
- $nin (not in): เลือกเอกสารที่ค่าของฟิลด์ ไม่อยู่ใน array ที่ระบุ
- [$nin: [null, ""]] : ไม่เป็น null และไม่ใช่สตริงว่าง
วิธีอื่นๆ ที่ทำได้เหมือนกัน
1. ใช้ $ne (Not Equal) ร่วมกับ $and
db.collection.find({
$and: [
{ UserName: { $ne: null } },
{ UserName: { $ne: "" } }
]
});
2. ใช้ $exists: true ร่วมกับ $ne: ""
db.collection.find({
UserName: { $exists: true, $ne: "" }
});
หมายเหตุ: $exists: true จะ match ทั้ง null และค่าว่าง ต้องใช้ $ne: "" คู่กับกัน
3. ใช้ $type (MongoDB 3.6+)
// เฉพาะ string ที่ไม่ว่าง
db.collection.find({
UserName: { $type: "string", $ne: "" }
});
การเปรียบเทียบ
| วิธี | Match null | Match "" | Match "value" | Missing field |
|---|---|---|---|---|
| $nin: [null, ""] | ❌ | ❌ | ✅ | ✅ |
| $exists: true, $ne: "" | ✅ | ❌ | ✅ | ❌ |
| $type: "string", $ne: "" | ❌ | ❌ | ✅ | ❌ |
คำแนะนำ
- ใช้ $nin: [null, ""] เมื่อต้องการ include ฟิลด์ที่ missing ด้วย
- ใช้ $exists: true, $ne: "" เมื่อต้องการบังคับให้ฟิลด์ต้องมีอยู่จริง
- ใช้ $type: "string" เมื่อต้องการเฉพาะ string type เท่านั้น