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.

CategoryBSON Types
MongoDB Version1.0+
Example Languagemongosh (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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏