{"library":"mongodb-memory-server","title":"MongoDB Memory Server for Testing","description":"MongoDB Memory Server is a robust Node.js library designed to provide a real, in-memory MongoDB server instance for testing purposes, eliminating the need for an external MongoDB installation. It automatically downloads the appropriate MongoDB binary to a local cache directory (`./node_modules/.cache`) upon package installation or first run, ensuring that tests are isolated and reproducible. The current stable version is 11.0.1. The package maintains an active release cadence, frequently pushing patches and minor updates, with major versions typically aligning with significant MongoDB binary version changes or Node.js LTS updates. It's particularly useful for integration tests, allowing developers to connect their preferred ODM (like Mongoose or Typegoose) or client library to a fresh, isolated MongoDB instance for each test suite, thereby preventing state leakage between tests and speeding up CI/CD pipelines.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mongodb-memory-server"],"cli":null},"imports":["import { MongoMemoryServer } from 'mongodb-memory-server';","import { MongoMemoryReplSet } from 'mongodb-memory-server';","await mongoServer.start();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { MongoMemoryServer } from 'mongodb-memory-server';\nimport { MongoClient } from 'mongodb';\n\nlet mongoServer: MongoMemoryServer;\nlet client: MongoClient;\n\nasync function setupDatabase() {\n  mongoServer = await MongoMemoryServer.create();\n  const mongoUri = mongoServer.getUri();\n  client = new MongoClient(mongoUri);\n  await client.connect();\n  console.log(`Connected to MongoDB at ${mongoUri}`);\n\n  const db = client.db('testdb');\n  const collection = db.collection('documents');\n\n  await collection.insertOne({ a: 1 });\n  const doc = await collection.findOne({ a: 1 });\n  console.log('Inserted and found document:', doc);\n}\n\nasync function teardownDatabase() {\n  if (client) {\n    await client.close();\n    console.log('MongoDB client closed.');\n  }\n  if (mongoServer) {\n    await mongoServer.stop();\n    console.log('MongoDB Memory Server stopped.');\n  }\n}\n\n(async () => {\n  try {\n    await setupDatabase();\n  } catch (error) {\n    console.error('Database operation failed:', error);\n  } finally {\n    await teardownDatabase();\n  }\n})();","lang":"typescript","description":"This quickstart demonstrates how to initialize, connect to, perform a basic operation, and cleanly shut down a MongoDB Memory Server instance using the official `mongodb` driver.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}