Boolean (True/False)
Last updated: 2026-10-09
Boolean (True/False)
Boolean holds true or false — ideal for flags and toggles (isActive, isDeleted). Query by matching the field value directly; note that a missing field differs from false, so use `$exists` to distinguish. Avoid 0/1 or 'yes'/'no' strings — they create type confusion.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ isActive: true }
db.users.find({ isActive: false })
db.users.find({ isActive: { $exists: false } })
Returns:The boolean true/false; queries return matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// Boolean: flag filtering
use shop
db.users.insertOne({ name: 'alice', isActive: true })
db.users.find({ isActive: true })
Output:
{ _id: ObjectId('...'), name: 'alice', isActive: true }
📌 Notes
- Only true/false; don't substitute 0/1 strings
- Missing field != false; distinguish with $exists
- Pair booleans with $cond for conditional aggregation
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QCan I index a boolean field?
AYes, but with only two values it has low cardinality; prefer a compound index with other fields.
QIs isActive:false the same as the field being absent?
ANo. Matching false returns docs whose value is false; absent fields match only with $exists:false.
QCan I use 0/1 instead of a boolean?
ANot recommended — mixing numbers and booleans breaks type checks in queries and aggregation.