MongoDB Memory Server Global 4.4

11.0.1 · active · verified Tue Apr 21

mongodb-memory-server-global-4.4 provides an isolated, in-memory MongoDB server instance specifically configured for MongoDB version 4.4, designed for automated testing environments. It automatically downloads the MongoDB 4.4 binary to a local cache directory upon installation, eliminating the need for a globally installed MongoDB. As of early 2025, the package is at version 11.0.1, part of an actively maintained project that sees frequent patch and minor releases, with major versions released periodically to align with Node.js and MongoDB version updates. Its key differentiator is the ease of use and version pinning for testing, contrasting with the base `mongodb-memory-server` package which requires explicit version configuration or defaults to a newer MongoDB release. This global package ensures consistent test environments tied to MongoDB 4.4.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to start an in-memory MongoDB 4.4 server, connect a native MongoDB client, perform a basic database operation, and then properly shut down both the client and the server.

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

async function runTestDatabase() {
  let mongoServer: MongoMemoryServer | null = null;
  let client: MongoClient | null = null;
  try {
    // Start a new in-memory MongoDB server configured for 4.4
    mongoServer = await MongoMemoryServer.create();
    const mongoUri = mongoServer.getUri();
    console.log(`MongoDB Memory Server started at: ${mongoUri}`);

    // Connect a MongoDB client to the in-memory server
    client = new MongoClient(mongoUri);
    await client.connect();
    console.log('MongoDB client connected successfully.');

    // Perform some database operations
    const db = client.db('testdb');
    const collection = db.collection('documents');
    await collection.insertOne({ name: 'Test Document', value: 1 });
    const docs = await collection.find({}).toArray();
    console.log('Inserted and found documents:', docs);

  } catch (error) {
    console.error('Error during test database operations:', error);
  } finally {
    // Ensure the client and server are always stopped
    if (client) {
      await client.close();
      console.log('MongoDB client closed.');
    }
    if (mongoServer) {
      await mongoServer.stop();
      console.log('MongoDB Memory Server stopped.');
    }
  }
}

runTestDatabase();

view raw JSON →