Boolean Values and Boolean Query Semantics
The BSON Boolean type represents either true or false.
Typical fields include:
isActivepublishedemailVerifiedisDeletedhasAccess
Example:
{
name: "Meera",
emailVerified: true,
isActive: false
}
Exact Boolean matching
db.users.find({ emailVerified: true });
This matches documents where emailVerified equals the Boolean value true.
The following value is not the same BSON type:
{ emailVerified: "true" }
The string "true" is text, not a Boolean.
Missing fields and Booleans
Consider these documents:
{ name: "Arun", isActive: true }
{ name: "Meera", isActive: false }
{ name: "Ravi" }
The third document has no isActive field. A query for { isActive: false } does not generally mean 'the field is false or absent'; it matches the explicit Boolean false value.
If you need to match missing fields, use $exists:
db.users.find({ isActive: { $exists: false } });
Boolean fields versus status fields
A Boolean is appropriate when the concept genuinely has two states. If a workflow has states such as draft, published, archived, and rejected, a string or another controlled representation may be more expressive than multiple Boolean flags.
For example, separate flags such as isDraft, isPublished, and isArchived can permit contradictory combinations unless the application or database enforces consistency.
A status field can model the mutually exclusive state directly:
{ status: "published" }
Use a schema validation rule or application-level validation when only a defined set of statuses is permitted.
db.booleanExamples.insertMany([
{ name: "Arun", isActive: true },
{ name: "Meera", isActive: false },
{ name: "Ravi" },
{ name: "Kavitha", isActive: "false" }
]);
// Explicit Boolean false.
db.booleanExamples.find({ isActive: false });
// Field is absent.
db.booleanExamples.find({ isActive: { $exists: false } });
// Field is stored as a string.
db.booleanExamples.find({ isActive: { $type: "string" } });false. These are distinct cases. The equality filter for Boolean false does not treat the string "false" as Boolean false and does not generally include documents where the field is absent.- Boolean values are
trueandfalse. - Strings such as
"true"and"false"are not Boolean values. - An absent Boolean field differs from an explicit false value.
- Use a status field when a concept has several mutually exclusive states.
- Validation can prevent accidental Boolean type inconsistencies.
Which query specifically finds documents where the `isActive` field is absent?