PyMongo In-Memory is a Python library that provides an ephemeral in-memory MongoDB server, primarily designed for testing and continuous integration environments. It achieves this by downloading and running a real `mongod` binary configured to use the in-memory storage engine. The library is actively maintained, with frequent updates that include support for newer MongoDB versions and bug fixes, typically released every few months.
pip install pymongo-inmemoryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `pymongo-inmemory.MongoClient` within a context manager. The server is automatically started upon entering the `with` block and stopped upon exiting. Basic CRUD operations are shown, connecting to a 'testdb' database and a 'test_collection'. You can specify the MongoDB version via environment variables or `setup.cfg`.
Explicitly configure the `storage_engine` parameter in your `setup.cfg` or environment variables if you encounter unexpected behavior with MongoDB versions > 6.0.
Always explicitly set `operating_system` (e.g., `PYMONGOIM__OPERATING_SYSTEM=ubuntu`) and `os_version` for MongoDB versions beyond 4.0.23 when running on Linux to ensure the correct binary is downloaded.
Do not use `pymongo-inmemory` for any scenario where data persistence across process restarts is required. It is strictly for ephemeral testing.
Ensure the `mongod_data_folder` is either a new, empty directory or one explicitly cleaned before starting the in-memory server. You can configure this via `PYMONGOIM__MONGOD_DATA_FOLDER` or `setup.cfg`.
Ensure network connectivity or pre-configure `use_local_mongod` if running in an isolated environment. The `download_folder` and `extract_folder` can also be customized.
Check logs for `pymongo-inmemory` (set `PYMONGOIM__DEBUG=True` for verbose output). Try specifying a different `mongod_port` (e.g., `PYMONGOIM__MONGOD_PORT=27018`). Ensure your system has internet access for the initial binary download. If on Linux with a newer MongoDB version, confirm `operating_system` and `os_version` are explicitly set.
Provide a new, empty directory for `mongod_data_folder` (e.g., `PYMONGOIM__MONGOD_DATA_FOLDER=/tmp/mongo_test_data`) or manually clear the contents of the conflicting `dbPath` before starting the server.
Optimize your queries to reduce memory footprint, use `allowDiskUse=True` in aggregation pipelines if applicable (though this would bypass the 'in-memory' aspect), or increase the system's available memory. For `pymongo-inmemory` specifically, you might need to adjust the `inMemorySizeGB` if it were configurable at that level (it's often handled by MongoDB's defaults).
Ensure all path-related or string-expected configuration values (e.g., `mongod_port` if directly passed, `download_folder`, `extract_folder`, `mongod_data_folder`) are passed as strings. For `mongod_port`, the library usually handles type coercion, but direct manipulation might bypass it. Review your configuration, especially if upgrading from older versions.