{"id":23286991,"url":"https://github.com/mohammadatikurrahman/portable-mongodb-setup","last_synced_at":"2026-05-14T20:06:10.417Z","repository":{"id":247553676,"uuid":"826145995","full_name":"MohammadAtikurRahman/portable-mongodb-setup","owner":"MohammadAtikurRahman","description":"This Setup includes the raw folder setup for MongoDB binaries and data directories, enabling MongoDB to run directly in your project without the need to install it via npm install portable-mongodb. Simply add the package files manually to your proj","archived":false,"fork":false,"pushed_at":"2024-11-10T04:47:11.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-19T15:59:11.140Z","etag":null,"topics":["mongodb","mongosee","portable-mongodb"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MohammadAtikurRahman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-09T07:00:24.000Z","updated_at":"2024-11-10T04:45:10.000Z","dependencies_parsed_at":"2025-04-06T15:41:52.946Z","dependency_job_id":"efd5ed50-4c19-4be2-99e2-9bbf72c97b58","html_url":"https://github.com/MohammadAtikurRahman/portable-mongodb-setup","commit_stats":null,"previous_names":["mohammadatikurrahman/embedding-mongodb","mohammadatikurrahman/portable-mongodb-setup"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/MohammadAtikurRahman/portable-mongodb-setup","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohammadAtikurRahman%2Fportable-mongodb-setup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohammadAtikurRahman%2Fportable-mongodb-setup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohammadAtikurRahman%2Fportable-mongodb-setup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohammadAtikurRahman%2Fportable-mongodb-setup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MohammadAtikurRahman","download_url":"https://codeload.github.com/MohammadAtikurRahman/portable-mongodb-setup/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MohammadAtikurRahman%2Fportable-mongodb-setup/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33041254,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-13T13:14:54.681Z","status":"online","status_checked_at":"2026-05-14T02:00:06.663Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongodb","mongosee","portable-mongodb"],"created_at":"2024-12-20T02:15:34.296Z","updated_at":"2026-05-14T20:06:10.400Z","avatar_url":"https://github.com/MohammadAtikurRahman.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Portable MongoDB Setup in Node.js\n\nA step-by-step guide to creating a portable MongoDB database for Node.js applications without requiring a full MongoDB installation. This setup combines `mongodb-memory-server` with `mongodb-binaries` to offer a solution that is both embedded and persistent across restarts, perfect for Electron apps and other portable applications.\n\n## Why Use Portable MongoDB?\n\n1. **Easy Setup**: No need to install MongoDB on the system, which simplifies deployment and sharing.\n2. **Persistent Storage**: Data remains intact across restarts using defined directories.\n3. **Ideal for Portable Apps**: Essential for applications that need portability and simplicity, such as Electron apps.\n\n## Prerequisites\n\n- **Node.js version**: 18\n- **MongoDB Binary version**: 4.0.28\n\n---\n\n## Installation\n\nInstall the necessary packages:\n\n```bash\nnpm install mongoose mongodb-memory-server path\n```\n\n## Setting Up the Portable MongoDB Solution\n\nIn this setup, `mongodb-memory-server` provides an embedded MongoDB instance, and we specify custom paths for binaries and data storage, enabling persistence.\n\n### Step 1: Configure Database Connection with Persistent Storage\n\nCreate a file named `connectToDatabase.js`:\n\n```javascript\nconst mongoose = require(\"mongoose\");\nconst { MongoMemoryServer } = require(\"mongodb-memory-server\");\nconst path = require(\"path\");\n\nasync function connectToDatabase() {\n  try {\n    const binaryPath = path.join(__dirname, \"./mongodb-binaries\");\n    const dbPath = path.join(__dirname, \"./mongodb-data\");\n\n    process.env.MONGOMS_SYSTEM_BINARY = path.join(binaryPath, \"mongod.exe\");\n\n    const mongod = new MongoMemoryServer({\n      instance: {\n        dbName: \"PortableDatabase\",\n        dbPath: dbPath,\n        storageEngine: \"wiredTiger\",\n        port: 27017,\n      },\n      binary: {\n        version: \"4.0.28\",\n        downloadDir: binaryPath,\n        mongodBinaryPath: path.join(binaryPath, \"mongod.exe\"),\n        skipMD5: true,\n        autoDownload: false,\n      },\n      autoStart: false,\n    });\n\n    await mongod.start();\n    const mongoUri = await mongod.getUri();\n    console.log(\"MongoDB Portable URI:\", mongoUri);\n\n    await mongoose.connect(mongoUri, {\n      serverSelectionTimeoutMS: 5000,\n      dbName: \"PortableDatabase\",\n    });\n\n    console.log(\"MongoDB connected with portable, persistent storage.\");\n  } catch (err) {\n    console.error(\"Error connecting to MongoDB:\", err);\n    throw err;\n  }\n}\n\nmodule.exports = connectToDatabase;\n```\n\n### Step 2: Create an Express Server\n\nCreate a `server.js` file to initialize Express and define routes:\n\n```javascript\nconst express = require(\"express\");\nconst app = express();\nconst connectToDatabase = require(\"./connectToDatabase\");\nconst mongoose = require(\"mongoose\");\n\napp.use(express.json());\n\nconst UserSchema = new mongoose.Schema({\n  user_name: String,\n  user_age: Number,\n  user_gender: String,\n});\nconst User = mongoose.model(\"User\", UserSchema);\n\napp.post(\"/api/add_user\", async (req, res) =\u003e {\n  try {\n    const { user_name, user_age, user_gender } = req.body;\n    const newUser = new User({ user_name, user_age, user_gender });\n    await newUser.save();\n    res.status(201).json({ message: \"User created successfully\", user: newUser });\n  } catch (error) {\n    console.error(\"Error creating user:\", error);\n    res.status(500).json({ message: \"Failed to create user\", error });\n  }\n});\n\napp.get(\"/api/get_users\", async (req, res) =\u003e {\n  try {\n    const users = await User.find();\n    res.status(200).json(users);\n  } catch (error) {\n    console.error(\"Error fetching users:\", error);\n    res.status(500).json({ message: \"Failed to fetch users\", error });\n  }\n});\n\nasync function startServer() {\n  try {\n    await connectToDatabase();\n    const server = app.listen(2000, () =\u003e {\n      console.log(\"Server running on http://localhost:2000\");\n    });\n\n    process.on(\"SIGINT\", async () =\u003e {\n      console.log(\"Shutting down server...\");\n      await mongoose.disconnect();\n      server.close();\n      console.log(\"Server shut down.\");\n      process.exit(0);\n    });\n  } catch (error) {\n    console.error(\"Error starting server:\", error);\n  }\n}\n\nstartServer();\n```\n\n### Step 3: Prepare MongoDB Binary\n\n1. **Download MongoDB Binary**: [MongoDB Download Center](https://www.mongodb.com/try/download/community) - Select version 4.0.28 for your OS.\n2. **Extract Files**: Unzip and locate the `mongod.exe` file.\n3. **Add to Project Directory**: Move `mongod.exe` and any other necessary files to your `mongodb-binaries` directory.\n\n### Step 4: Directory Structure\n\nOrganize your project like this:\n\n```\nportable-mongodb-setup/\n├── model\n│   └── user.js\n├── mongodb-binaries # MongoDB binaries\n├── mongodb-data # MongoDB data storage\n├── node_modules\n├── .gitignore\n├── connectToDatabase.js\n├── server.js\n├── package.json\n└── README.md\n```\n\n### Step 5: Running the App\n\nStart the server:\n\n```bash\nnode server.js\n```\n\nTo test:\n\n1. Send a POST request to `/api/add_user` with JSON data.\n2. Restart the server.\n3. Send a GET request to `/api/get_users` to confirm data persistence.\n\n---\n\n## Resources\n\n- **GitHub Project Setup**: [portable-mongodb-setup](https://github.com/MohammadAtikurRahman/portable-mongodb-setup)\n- **NPM Package**: [portable-mongodb](https://www.npmjs.com/package/portable-mongodb)\n\n---\n\nWith this setup, enjoy the portability of MongoDB without a system installation. Perfect for Node.js apps requiring embedded and persistent storage!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohammadatikurrahman%2Fportable-mongodb-setup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohammadatikurrahman%2Fportable-mongodb-setup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohammadatikurrahman%2Fportable-mongodb-setup/lists"}