Official MongoDB Python driver. Synchronous client for MongoDB. v4.0 removed all APIs deprecated in v3.x — insert(), update(), remove(), count(), save() all gone. Current version: 4.16.0 (Mar 2026). Python 3.9+ required (3.8 dropped in v4.11). AsyncMongoClient added as GA in v4.13. Most LLM-generated pymongo code uses v3 patterns that break on v4.
pip install pymongoVerified import paths — ran on the pinned version, not inferred.
Minimal pymongo 4.x CRUD operations.
insert_one({'key': 'value'}) or insert_many([{...}, {...}])update_one(filter, update) or update_many(filter, update)
delete_one(filter) or delete_many(filter)
count_documents(filter) — note: count_documents({}) counts all docs. estimated_document_count() for fast approximate count.insert_one() for new docs, replace_one(filter, doc, upsert=True) for upsert.
find_one_and_update(), find_one_and_replace(), or find_one_and_delete()
pip install 'pymongo[srv]'
Use Motor >= 3.6 with PyMongo >= 4.9. Or use native AsyncMongoClient (GA in 4.13) instead of Motor.
Use Python 3.9+
from datetime import datetime, timezone; datetime.now(tz=timezone.utc)
Verify the `mongodb+srv` connection string hostname and ensure its corresponding SRV DNS record exists and is correctly configured with your DNS provider.