Skip to content

API Reference

Aryeh Citron edited this page May 12, 2026 · 2 revisions

API Reference

Core Classes

InMemoryMongo

Static factory for creating in-memory MongoDB instances.

Method Returns Description
Create<T>(collectionName) InMemoryMongoResult<T> Creates a single-collection setup
Builder() InMemoryMongoBuilder Creates a multi-database builder

InMemoryMongoResult<T>

Property Type Description
Client IMongoClient The in-memory client
Database IMongoDatabase The in-memory database
Collection IMongoCollection<T> The in-memory collection

InMemoryMongoBuilder

Method Returns Description
AddDatabase(name, configure) InMemoryMongoBuilder Add a database with collections
Build() IMongoClient Build the multi-database setup

DatabaseBuilder

Method Returns Description
AddCollection<T>(name) DatabaseBuilder Add a typed collection

InMemoryMongoClient : IMongoClient

Method Description
GetDatabase(name) Get or create an in-memory database
ListDatabases() List all databases
ListDatabaseNames() List all database names
DropDatabase(name) Drop a database
StartSession() Start a client session (transactions)
Watch() Open a client-level change stream
WithReadConcern() / WithWriteConcern() / WithReadPreference() Return configured client (settings stored, not enforced)
Dispose() No-op. IMongoClient extends IDisposable in Driver 3.x

InMemoryMongoDatabase : IMongoDatabase

Method Description
GetCollection<T>(name) Get or create a collection
CreateCollection(name, options) Create a collection (capped, validation, time series)
DropCollection(name) Drop a collection
ListCollections() List all collections
ListCollectionNames() List collection names
RenameCollection(old, new) Rename a collection
RunCommand<T>(command) Execute a database command
Aggregate<T>(pipeline) Database-level aggregation
CreateView(viewName, source, pipeline) Create a view
Watch() Open a database-level change stream
ExportState() / ImportState(json) Export/import all collection data as JSON
ExportStateToFile(path) / ImportStateFromFile(path) File-based export/import
ClearDocuments() Clear all documents from all collections

InMemoryMongoCollection<T> : IMongoCollection<T>

CRUD

Method Description
InsertOne / InsertOneAsync Insert a single document
InsertMany / InsertManyAsync Insert multiple documents
Find / FindSync / FindAsync Query documents
UpdateOne / UpdateOneAsync Update first matching document
UpdateMany / UpdateManyAsync Update all matching documents
ReplaceOne / ReplaceOneAsync Replace first matching document
DeleteOne / DeleteOneAsync Delete first matching document
DeleteMany / DeleteManyAsync Delete all matching documents
FindOneAndDelete / FindOneAndReplace / FindOneAndUpdate Atomic find-and-modify
BulkWrite / BulkWriteAsync Execute multiple write operations
CountDocuments / EstimatedDocumentCount Count documents
Distinct / DistinctMany Get distinct field values

Aggregation

Method Description
Aggregate / AggregateAsync Execute aggregation pipeline
AggregateToCollection Execute pipeline, write results to collection

Change Streams

Method Description
Watch / WatchAsync Open a change stream on this collection

Indexes

Property Description
Indexes IMongoIndexManager<T> - create, drop, list indexes

Testing Utilities

Property Type Description
FaultInjector Action<string, BsonDocument?>? Hook to simulate failures
OperationLog OperationLog Records all operations for test assertions

InMemoryGridFSBucket

Method Description
UploadFromBytes / UploadFromBytesAsync Upload file from byte array
UploadFromStream / UploadFromStreamAsync Upload file from stream
DownloadAsBytes / DownloadAsBytesAsync Download file as byte array
DownloadToStream / DownloadToStreamAsync Download file to stream
DownloadAsBytesByName / DownloadToStreamByName Download by filename
Delete / DeleteAsync Delete a file
Rename / RenameAsync Rename a file
Find / FindAsync Find files by filter
Drop / DropAsync Drop the entire bucket

OperationLog

Method Description
GetAll() Get all recorded operations
GetByType(type) Get operations by type (e.g., "InsertOne")
Clear() Clear all recorded operations
Count Number of recorded operations

OperationRecord

Property Type Description
Type string Operation type (e.g., "InsertOne", "UpdateOne")
Filter BsonDocument? The filter used
Update BsonDocument? The update document
Document BsonDocument? The document involved
Pipeline BsonDocument[]? The pipeline stages
Timestamp DateTimeOffset When the operation occurred

DI Extension Methods

ServiceCollectionExtensions

Method Description
UseInMemoryMongoDB(options) Replace IMongoClient + IMongoDatabase + IMongoCollection
UseInMemoryMongoCollections(options) Replace only IMongoCollection registrations

See Also

Clone this wiki locally