NotImplementedError: Collection objects do not implement truth value testing MongoDB Origin: Data Last updated 5 Nov 2026

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.

Exact message you may see 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
Seen on: MongoDB Python

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 y on a Database or Collection
  • Code written for PyMongo 3
  • Optional dependency checks
  • Cached connection checks
  • Conditional default assignments

⚡ Quick fix

  1. if db is not None:
  2. Use explicit sentinel checks
  3. Search your code for if collection / if db

Detailed fix by platform

Python

  1. if collection is None: collection = client["app"]["users"]

How to diagnose

  1. Pattern — Truthiness used?
  2. Replace — is None?
  3. 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:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

Atlas: bad auth: Authentication failed — MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password) (MongoDB)Atlas: bad auth: Auth…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)BSONObj size is inval…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 (MongoDB)MongoDB Atlas: connec…pymongo.errors.BulkWriteError: batch op errors occurred — PyMongo: pymongo.errors.BulkWriteError: batch op errors occurred (MongoDB)pymongo.errors.BulkWr…pymongo.errors.ServerSelectionTimeoutError — PyMongo: pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s (MongoDB)pymongo.errors.Server…Sort/Group exceeded memory limit of 100 MB (allowDiskUse) — MongoServerError: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in (MongoDB)Sort/Group exceeded m…TypeError: NoneType — TypeError: 'NoneType' object is not subscriptable (Python)TypeError: NoneTypeNotImplem…

Explore the map of this error Full error map →

🧠 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.