PyMongo: NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None
You used a Database/Collection object in an if (if db:), which PyMongo forbids.
NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not NoneCollection objects do not implement truth value testing or bool()do not implement truth value testing or bool()Please compare with None instead: collection is not None Meaning
Compare explicitly: if db is not None:. PyMongo 4+ raises this to avoid ambiguous truthiness. Update patterns such as db = client.db or default and if collection:.
Common causes
if db:/if collection:/x or yon a Database or Collection- Code written for PyMongo 3
- Optional dependency checks
- Cached connection checks
- Conditional default assignments
⚡ Quick fix
if db is not None:- Use explicit sentinel checks
- Search your code for
if collection/if db
Detailed fix by platform
Python
if collection is None: collection = client["app"]["users"]
How to diagnose
- Pattern — Truthiness used?
- Replace — is None?
- Version — PyMongo 4?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Atlas: bad auth: Authentication failed MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password)
- BSONObj size is invalid / exceeds maximum document size (16MB) MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB)
- MongoDB Atlas: connection timed out — IP not whitelisted MongoServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted
Most viewed in MongoDB
- MongoParseError MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
- Document failed validation MongoDB: MongoServerError: Document failed validation (code 121)
- bad auth : Authentication failed MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026