-
Notifications
You must be signed in to change notification settings - Fork 0
How It Works
InMemoryEmulator.MongoDB provides in-memory implementations of the MongoDB .NET Driver's key interfaces:
| Interface | In-Memory Implementation |
|---|---|
IMongoClient |
InMemoryMongoClient |
IMongoDatabase |
InMemoryMongoDatabase |
IMongoCollection<T> |
InMemoryMongoCollection<T> |
IMongoIndexManager<T> |
InMemoryIndexManager<T> |
IAsyncCursor<T> |
InMemoryAsyncCursor<T> |
Unlike the CosmosDB emulator (which intercepts HTTP handlers), InMemoryEmulator.MongoDB works through direct interface implementation. The MongoDB .NET Driver is designed around interfaces, making this straightforward:
┌─────────────────────┐ ┌──────────────────────┐
│ Your Application │ │ Your Application │
│ │ │ │
│ IMongoClient │ │ IMongoClient │
│ ↓ │ │ ↓ │
│ MongoClient │ │ InMemoryMongoClient │
│ ↓ │ │ ↓ │
│ TCP → MongoDB │ │ ConcurrentDictionary │
│ │ │ (in-process) │
│ PRODUCTION │ │ TESTS │
└─────────────────────┘ └──────────────────────┘
Your production code programs against IMongoClient, IMongoDatabase, and IMongoCollection<T>. In tests, the DI container swaps in the in-memory implementations. Zero production code changes required.
Documents are stored as BsonDocument objects in a ConcurrentDictionary<BsonValue, BsonDocument>, keyed by _id. This provides:
- Thread safety — Safe for parallel test execution
- BSON fidelity — Documents stored in the same format as real MongoDB
- Deep cloning — Each read returns a clone, preventing accidental mutation
The emulator includes a full query processing pipeline:
-
Filter evaluation —
BsonFilterEvaluatorprocessesFilterDefinition<T>(all comparison, logical, array, element, regex, geospatial, and bitwise operators) -
Update application —
BsonUpdateEvaluatorappliesUpdateDefinition<T>(all field, array, and bitwise update operators) -
Sort evaluation —
BsonSortEvaluatorhandles multi-field sorting with BSON type comparison order -
Projection —
BsonProjectionEvaluatorsupports inclusion/exclusion,$slice,$elemMatch,$meta -
Aggregation —
AggregationPipelineExecutorprocesses 34 pipeline stages with 100+ expression operators
The MongoDB .NET Driver renders FilterDefinition<T>, UpdateDefinition<T>, and PipelineDefinition<T, T> into BSON documents. The emulator then evaluates these rendered BSON documents against the in-memory store using LINQ-to-Objects.
Indexes are stored as metadata and actively enforced:
- Unique indexes — Checked on every insert, update, and replace operation
- Sparse indexes — Documents missing the indexed field are excluded from uniqueness checks
- Partial filter indexes — Only documents matching the filter expression are subject to the constraint
- TTL indexes — Documents are lazily evicted when queries are executed
Change streams are implemented via an internal change log. Each write operation records a DocumentChangeRecord with sequence number, operation type, full document, and before-image. Change stream cursors tail this log.
- Choosing Your Approach — Decision guide for setup patterns
- Setup Guide — DI integration patterns
- Features — Complete feature reference
Getting Started
Integration & Dependency Injection
Data Management
Reference
- Feature Comparison
- Features
- Filter & Update Operators
- Aggregation Pipeline
- LINQ Support
- API Reference
Help