Skip to content
Aryeh Citron edited this page May 12, 2026 · 4 revisions

InMemoryEmulator.MongoDB

A high-fidelity, in-process fake for the MongoDB .NET/C# Driver — designed for fast, reliable integration and unit tests without Docker, Testcontainers, or Mongo2Go.

Why?

Problem Solution
Docker-based MongoDB is slow to start In-memory, instant startup (~0ms)
Mongo2Go downloads & manages mongod binaries Zero external dependencies
Testcontainers require Docker Desktop Runs anywhere — CI, laptops, codespaces
Mocking IMongoCollection<T> is tedious and fragile Drop-in replacement with real query semantics
Real MongoDB flakes on CI (timeouts, port conflicts) Deterministic, no network, no ports

Quick Start

NuGet

dotnet add package InMemoryEmulator.MongoDB

No-DI (unit tests)

var result = InMemoryMongo.Create<MyDocument>("myCollection");
var collection = result.Collection;

await collection.InsertOneAsync(new MyDocument { Name = "Test" });
var found = await collection.Find(x => x.Name == "Test").FirstOrDefaultAsync();

With DI (integration tests)

// In your test's ConfigureTestServices:
services.UseInMemoryMongoDB(options =>
{
    options.DatabaseName = "testdb";
    options.AddCollection<MyDocument>("myCollection");
});

What's Supported

  • Full CRUD — Insert, Find, Update, Replace, Delete, Upsert, BulkWrite
  • Aggregation Pipeline — 34 stages, 100+ expression operators
  • Indexes — Unique (enforced), Compound, Sparse, Partial Filter, TTL with lazy eviction
  • Change Streams — Collection, database, and client-level, with resume tokens
  • Transactions — StartTransaction / Commit / Abort with snapshot isolation
  • GridFS — Upload, download, find, rename, delete
  • Schema Validation$jsonSchema enforcement on writes
  • LINQAsQueryable() with LINQ-to-Objects
  • Fault Injection — Simulate errors on any operation
  • State Persistence — Export / Import JSON snapshots

See Features for the complete list.

Pages

Clone this wiki locally