Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Truy vấn MongoDB: trường không tồn tại, null hoặc false

Để tìm document mà một trường thỏa mãn một trong 3 điều kiện: không tồn tại, null, hoặc false, bạn sử dụng toán tử $in với mảng [false, null].

Cú pháp

db.collection.find({ "fieldName": { $in: [false, null] } })

Cách này hoạt động vì MongoDB coi trường không tồn tại (missing) khớp với null trong query {field: null}.

Ví dụ minh họa

Tạo collection và dữ liệu

> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":1,"StudentName":"Larry"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9010215705caea966c557f") }
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":2,"StudentName":"Mike","hasAgeGreaterThanOrEqualTo18":true});
{ "acknowledged" : true, "insertedId" : ObjectId("5c90106a5705caea966c5580") }
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":3,"StudentName":"Carol","hasAgeGreaterThanOrEqualTo18":false});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9010795705caea966c5581") }
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":4,"StudentName":"Sam","hasAgeGreaterThanOrEqualTo18":null});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9010865705caea966c5582") }
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":5,"StudentName":"David","hasAgeGreaterThanOrEqualTo18":false});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9010945705caea966c5583") }
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":6,"StudentName":"Chris","hasAgeGreaterThanOrEqualTo18":null});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9010a45705caea966c5584") }
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":7,"StudentName":"Robert","hasAgeGreaterThanOrEqualTo18":true});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9010b05705caea966c5585") }

Xem tất cả documents

> db.selectMongoDBDocumentsWithSomeCondition.find().pretty();
{
    "_id" : ObjectId("5c9010215705caea966c557f"),
    "StudentId" : 1,
    "StudentName" : "Larry"
}
{
    "_id" : ObjectId("5c90106a5705caea966c5580"),
    "StudentId" : 2,
    "StudentName" : "Mike",
    "hasAgeGreaterThanOrEqualTo18" : true
}
{
    "_id" : ObjectId("5c9010795705caea966c5581"),
    "StudentId" : 3,
    "StudentName" : "Carol",
    "hasAgeGreaterThanOrEqualTo18" : false
}
{
    "_id" : ObjectId("5c9010865705caea966c5582"),
    "StudentId" : 4,
    "StudentName" : "Sam",
    "hasAgeGreaterThanOrEqualTo18" : null
}
{
    "_id" : ObjectId("5c9010945705caea966c5583"),
    "StudentId" : 5,
    "StudentName" : "David",
    "hasAgeGreaterThanOrEqualTo18" : false
}
{
    "_id" : ObjectId("5c9010a45705caea966c5584"),
    "StudentId" : 6,
    "StudentName" : "Chris",
    "hasAgeGreaterThanOrEqualTo18" : null
}
{
    "_id" : ObjectId("5c9010b05705caea966c5585"),
    "StudentId" : 7,
    "StudentName" : "Robert",
    "hasAgeGreaterThanOrEqualTo18" : true
}

Truy vấn: hasAgeGreaterThanOrEqualTo18 là false, null, hoặc không tồn tại

> db.selectMongoDBDocumentsWithSomeCondition.find({ 
    "hasAgeGreaterThanOrEqualTo18": {$in: [false, null]} 
}).pretty();
{
    "_id" : ObjectId("5c9010215705caea966c557f"),
    "StudentId" : 1,
    "StudentName" : "Larry"
}
{
    "_id" : ObjectId("5c9010795705caea966c5581"),
    "StudentId" : 3,
    "StudentName" : "Carol",
    "hasAgeGreaterThanOrEqualTo18" : false
}
{
    "_id" : ObjectId("5c9010865705caea966c5582"),
    "StudentId" : 4,
    "StudentName" : "Sam",
    "hasAgeGreaterThanOrEqualTo18" : null
}
{
    "_id" : ObjectId("5c9010945705caea966c5583"),
    "StudentId" : 5,
    "StudentName" : "David",
    "hasAgeGreaterThanOrEqualTo18" : false
}
{
    "_id" : ObjectId("5c9010a45705caea966c5584"),
    "StudentId" : 6,
    "StudentName" : "Chris",
    "hasAgeGreaterThanOrEqualTo18" : null
}

Kết quả 5 document: Larry (missing), Carol (false), Sam (null), David (false), Chris (null). Mike và Robert (true) bị loại.

Giải thích cơ chế

DocumentGiá trị trườngKhớp $in [false, null]?
Larrymissing✅ Có (missing = null)
Miketrue❌ Không
Carolfalse✅ Có
Samnull✅ Có
Davidfalse✅ Có
Chrisnull✅ Có
Roberttrue❌ Không

Cách khác: Tách biệt rõ ràng

Nếu cần phân biệt rõ missing vs null vs false:

// Chỉ false
{ field: false }

// Chỉ null (có trường)
{ field: { $exists: true, $eq: null } }

// Chỉ missing
{ field: { $exists: false } }

// Tất cả 3 (cách tách biệt)
{ $or: [
    { field: false },
    { field: { $exists: true, $eq: null } },
    { field: { $exists: false } }
] }
Lưu ý: Cách dùng $in: [false, null] tận dụng hành vi mặc định của MongoDB (missing = null). Đây là cách viết gọn nhất nhưng hiểu rõ cơ chế để tránh bug.