In-memory drop-in replacements for pymongo Collection, Database, and MongoClient.
These classes mirror the pymongo interfaces used by Event, Experiment, Data, and Workcell managers, enabling the MADSci stack to run without a MongoDB server.
Usage::
client = InMemoryMongoClient()
db = client["my_database"]
collection = db["events"]
collection.insert_one({"_id": "1", "name": "test"})
doc = collection.find_one({"_id": "1"})Classes¶
InMemoryCollection(name: str)Drop-in replacement for
pymongo.collection.Collection.Supports:
insert_one,find_one,find,update_one,update_many,replace_one,delete_one,delete_many,count_documents,create_index,drop_index,index_information.Initialize a named in-memory collection.
Methods¶
count_documents(self, filter_query: Optional[dict[str, Any]] = None) ‑> intCount documents matching the filter.
create_index(self, keys: Any, **kwargs: Any) ‑> strCreate an index (no-op for in-memory; records metadata only).
delete_many(self, filter_query: dict[str, Any]) ‑> madsci.common.local_backends.inmemory_collection.InMemoryDeleteResultDelete all documents matching the filter.
delete_one(self, filter_query: dict[str, Any]) ‑> madsci.common.local_backends.inmemory_collection.InMemoryDeleteResultDelete the first document matching the filter.
drop_index(self, name: str) ‑> NoneRemove an index by name.
find(self, filter_query: Optional[dict[str, Any]] = None, projection: Optional[dict[str, Any]] = None) ‑> madsci.common.local_backends.inmemory_collection.InMemoryCursorReturn a cursor over documents matching the filter.
Args: filter_query: MongoDB-style query filter. projection: Optional field projection passed to the cursor.
find_one(self, filter_query: Optional[dict[str, Any]] = None, projection: Optional[dict[str, Any]] = None) ‑> dict[str, typing.Any] | NoneReturn the first document matching the filter, or
None.Args: filter_query: MongoDB-style query filter. projection: Optional field projection (e.g.
{"_id": 1}for inclusion or{"field": 0}for exclusion).index_information(self) ‑> dict[str, typing.Any]Return metadata for all indexes.
insert_one(self, document: dict[str, Any]) ‑> madsci.common.local_backends.inmemory_collection.InMemoryInsertResultInsert a single document and return the result.
Raises
pymongo.errors.DuplicateKeyErrorif the document violates a unique index constraint.list_indexes(self) ‑> list[dict[str, typing.Any]]Return a list of index info dicts, matching pymongo’s
Collection.list_indexes().replace_one(self, filter_query: dict[str, Any], replacement: dict[str, Any]) ‑> madsci.common.local_backends.inmemory_collection.InMemoryUpdateResultReplace the first document matching the filter with a new document.
The
_idfield of the original document is preserved.update_many(self, filter_query: dict[str, Any], update: dict[str, Any]) ‑> madsci.common.local_backends.inmemory_collection.InMemoryUpdateResultUpdate all documents matching the filter.
update_one(self, filter_query: dict[str, Any], update: dict[str, Any]) ‑> madsci.common.local_backends.inmemory_collection.InMemoryUpdateResultUpdate the first document matching the filter.
InMemoryCursor(documents: list[dict[str, Any]], projection: Optional[dict[str, Any]] = None)Mimics
pymongo.cursor.Cursorwith sort/skip/limit/to_list chaining.Initialize with a list of documents to iterate over.
Methods¶
limit(self, count: int) ‑> madsci.common.local_backends.inmemory_collection.InMemoryCursor- Limit the number of results returned.
skip(self, count: int) ‑> madsci.common.local_backends.inmemory_collection.InMemoryCursor- Skip the first count results.
sort(self, key: Any, direction: int = 1) ‑> madsci.common.local_backends.inmemory_collection.InMemoryCursor- Sort results. Accepts a string key or list of (key, direction) tuples.
to_list(self, length: Optional[int] = None) ‑> list[dict[str, typing.Any]]- Materialise the cursor to a list.
InMemoryDatabase(name: str = 'test', client: Optional[InMemoryMongoClient] = None)Drop-in replacement for
pymongo.database.Database.Access collections via
db["collection_name"].Initialize a named in-memory database.
Instance variables¶
client: InMemoryMongoClientReturn the parent
InMemoryMongoClient.Mirrors
pymongo.database.Database.client, used by health checks likedb.client.admin.command("ping").
Methods¶
command(self, cmd: str, **_kwargs: Any) ‑> dict[str, typing.Any]Execute a database command (only
pingis supported).create_collection(self, name: str) ‑> madsci.common.local_backends.inmemory_collection.InMemoryCollectionExplicitly create a collection.
If the collection already exists, returns the existing one.
drop_collection(self, name: str) ‑> NoneDrop a collection by name, removing all its data.
list_collection_names(self) ‑> list[str]Return the names of all collections that have been created.
InMemoryDeleteResult(deleted_count: int = 0)Mimics
pymongo.results.DeleteResult.Initialize with the deletion count.
InMemoryInsertResult(inserted_id: Any)Mimics
pymongo.results.InsertOneResult.Initialize with the inserted document’s ID.
InMemoryMongoClient(*_args: Any, **_kwargs: Any)Drop-in replacement for
pymongo.MongoClient.Access databases via
client["database_name"]. Providesclient.admin.command("ping")for health checks.Initialize the in-memory Mongo client.
Methods¶
close(self) ‑> None- Close the client (no-op for in-memory).
InMemoryUpdateResult(matched_count: int = 0, modified_count: int = 0)Mimics
pymongo.results.UpdateResult.Initialize with match and modification counts.