mongomock
Fake pymongo stub for testing simple MongoDB-dependent code
What it is and what it does
Mongomock is a testing library that replaces pymongo's MongoClient with an in-memory mock, allowing you to test MongoDB-dependent code without setting up or managing a real MongoDB instance. It implements enough of the MongoDB API to behave like a real collection for typical query and update operations, making tests faster and more portable across CI environments and local machines.
The library is designed for functional testing rather than as a perfect replica of MongoDB. When a feature is not yet implemented, it raises NotImplementedError rather than silently behaving differently from production pymongo, ensuring your tests fail where production code would fail. It supports both direct instantiation (creating a mock client in your test) and decorator-based patching (intercepting pymongo.MongoClient calls in code under test).
Use it for:
- Unit test code that queries and updates MongoDB collections without spinning up a test database.
- Run test suites in CI/CD pipelines where MongoDB infrastructure is unavailable or undesirable.
- Validate application logic against MongoDB operations in isolation, comparing expected vs. actual collection state.
- Migrate code from pymongo v3 to v4 by running tests against both versions to catch API incompatibilities early.
- Mock MongoDB interactions in decorator-patched functions that create their own pymongo connections.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Mongomock provides an in-memory mock of MongoDB collections for testing Python code that uses pymongo, without requiring a running MongoDB instance.
Yes. Mongomock is production-stable, widely used, has no known vulnerabilities, and solves a real testing problem with low install friction. The aging maintenance status is a minor concern but the repository remains active and the ISC license is permissive. Install it if you test pymongo-dependent code.
Install
mongomock on PyPI
pip
pip install mongomockuv
uv add mongomockpoetry
poetry add mongomockInstalling mongomock
Before you install
Low install friction: pure Python wheel with only three runtime dependencies (packaging, pytz, sentinels). Last release was 636 days ago; repository is active and not archived, though maintenance status is aging.
License in practice
ISC License (permissive): you may use, modify, and distribute mongomock freely in commercial and private projects with minimal restrictions—only requiring preservation of copyright and license notices.
Quickstart
import mongomock
client = mongomock.MongoClient()
db = client.test_db
collection = db.test_collection
collection.insert_one({'name': 'test', 'value': 1})
result = collection.find_one({'name': 'test'})
print(result)
Verify before relying
- Completeness of MongoDB API coverage relative to pymongo v4—the description notes TDD-driven feature addition but does not enumerate which operations are supported.
- Whether the package works with pymongo v5 or later versions beyond v4.
- Monthly download volume and ranking position reflect current popularity but may not indicate active maintenance frequency.
Package facts
| License | ISC License Copyright (c) 2024, Monogomock Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above… (full text in the JSON record) (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 3 — packaging, pytz, sentinels |
| Maintenance | aging — 636 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 7,923,784/month — #1,680 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: mongomock-4.3.0-py2.py3-none-any.whl
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
mongomock-motorProvides an async mock client for MongoDB that…
permissive · top 5,000 on PyPI
pymongo_inmemoryProvides an in-memory MongoDB instance for…
permissive · top 15,000 on PyPI
pymongoPyMongo is the official MongoDB Python driver,…
permissive · top 1,000 on PyPI
pymongoexplainWraps PyMongo's Collection class to explain…
permissive · top 15,000 on PyPI
Flask-PyMongoFlask-PyMongo integrates MongoDB database…
permissive · top 15,000 on PyPI
pymongo-auth-awsEnables PyMongo to authenticate to MongoDB…
permissive · top 5,000 on PyPI
pymongo-search-utilsProvides utility functions for vector search…
permissive · top 15,000 on PyPI
pymongocryptPyMongoCrypt provides Python bindings for…
permissive · top 5,000 on PyPI
bsonEncodes and decodes BSON (Binary JSON) data…
permissive · top 5,000 on PyPI
pymongoarrowPyMongoArrow converts MongoDB query results…
permissive · top 15,000 on PyPI