Skip to content

How It Works

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

How It Works

Architecture Overview

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>

How Interface Interception Works

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.

Internal Storage

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

Query Processing

The emulator includes a full query processing pipeline:

  1. Filter evaluationBsonFilterEvaluator processes FilterDefinition<T> (all comparison, logical, array, element, regex, geospatial, and bitwise operators)
  2. Update applicationBsonUpdateEvaluator applies UpdateDefinition<T> (all field, array, and bitwise update operators)
  3. Sort evaluationBsonSortEvaluator handles multi-field sorting with BSON type comparison order
  4. ProjectionBsonProjectionEvaluator supports inclusion/exclusion, $slice, $elemMatch, $meta
  5. AggregationAggregationPipelineExecutor processes 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.

Index Enforcement

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

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.

See Also

Clone this wiki locally