{"library":"mongodb-memory-server-global","title":"MongoDB Memory Server Global","description":"MongoDB Memory Server Global (mongodb-memory-server-global) provides a convenient, pre-configured way to set up and manage an in-memory MongoDB instance for testing and development. As of version 11.0.1, it automates the download and caching of the latest MongoDB server binaries (defaulting to 8.2.x since v11) to a user's local cache directory (`~/.cache/mongodb-binaries`) upon installation. This package acts primarily as an installation wrapper, simplifying the environment setup for its counterpart, `mongodb-memory-server`, which offers the actual programmatic API to start, stop, and manage the in-memory database instances. It aims for a stable release cadence, with patch and minor updates released frequently, and major versions introducing significant breaking changes and updated MongoDB defaults. Key differentiators include its ease of setup for CI/CD environments and its ability to quickly spin up isolated database instances without requiring a pre-installed MongoDB server.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mongodb-memory-server-global"],"cli":null},"imports":["import { MongoMemoryServer } from 'mongodb-memory-server'","import { MongoMemoryReplSet } from 'mongodb-memory-server'","import { start } from 'mongodb-memory-server'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { MongoMemoryServer } from 'mongodb-memory-server';\nimport mongoose from 'mongoose';\n\nasync function runInMemoryDbTest() {\n  // mongodb-memory-server-global ensures binaries are available.\n  // MongoMemoryServer handles the in-memory instance.\n  const mongod = await MongoMemoryServer.create();\n  const uri = mongod.getUri();\n\n  console.log(`MongoDB Memory Server started at: ${uri}`);\n\n  try {\n    await mongoose.connect(uri);\n    console.log('Mongoose connected to in-memory MongoDB');\n\n    const UserSchema = new mongoose.Schema({ name: String, email: String });\n    const User = mongoose.model('User', UserSchema);\n\n    const newUser = await User.create({ name: 'Test User', email: 'test@example.com' });\n    console.log('User created:', newUser);\n\n    const foundUser = await User.findOne({ name: 'Test User' });\n    console.log('User found:', foundUser);\n\n  } catch (error) {\n    console.error('Error during test:', error);\n  } finally {\n    await mongoose.disconnect();\n    console.log('Mongoose disconnected.');\n    await mongod.stop();\n    console.log('MongoDB Memory Server stopped.');\n  }\n}\n\nrunInMemoryDbTest();\n","lang":"typescript","description":"Demonstrates starting an in-memory MongoDB instance, connecting with Mongoose, performing basic CRUD operations, and then cleaning up the connection and server. This setup implicitly relies on `mongodb-memory-server-global` having pre-downloaded the necessary binaries.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}