MongoDB Memory Server Core

11.0.1 · active · verified Sun Apr 19

MongoDB Memory Server Core (v11.0.1) is a library designed to launch an in-memory MongoDB instance for integration and unit testing. This core package differs from the full `mongodb-memory-server` by *not* including the automatic download and management of MongoDB binaries, requiring users or other wrapper packages to provide the MongoDB executable. It enables developers to create isolated, disposable database environments programmatically, preventing test interference and ensuring a clean state for each run. The library is actively maintained with frequent updates, including version 11.0.0 and subsequent patches released in December 2025, and currently supports Node.js versions 20.19.0 and higher. Its primary use case is providing a robust, isolated MongoDB server for various ODM and client libraries within a testing suite.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates starting an in-memory MongoDB server, connecting with the official driver, performing a basic insert and find operation, and properly shutting down the client and server.

import { MongoMemoryServer } from 'mongodb-memory-server-core';
import { MongoClient } from 'mongodb';

async function runInMemoryMongo() {
  let mongod: MongoMemoryServer | undefined;
  let client: MongoClient | undefined;

  try {
    mongod = await MongoMemoryServer.create();
    const uri = mongod.getUri();
    console.log(`MongoDB Memory Server started at: ${uri}`);

    client = new MongoClient(uri);
    await client.connect();
    console.log('Connected to MongoDB Memory Server.');

    const db = client.db('testdb');
    const collection = db.collection('documents');

    await collection.insertOne({ name: 'Test Document', value: 123 });
    const doc = await collection.findOne({ name: 'Test Document' });
    console.log('Found document:', doc);

  } catch (error) {
    console.error('Error during in-memory MongoDB operation:', error);
  } finally {
    if (client) {
      await client.close();
      console.log('MongoDB client closed.');
    }
    if (mongod) {
      await mongod.stop();
      console.log('MongoDB Memory Server stopped.');
    }
  }
}

runInMemoryMongo();

view raw JSON →