Mongo Seeding is a versatile tool designed for populating MongoDB databases, offering flexibility through its command-line interface (CLI), a programmatic JavaScript/TypeScript library, and a Docker image. It enables developers to define import data using JavaScript, TypeScript, or JSON files, allowing for dynamic data generation and logic, a key differentiator from simpler tools like `mongoimport` which primarily handle static JSON. The current stable version is 4.0.2, and the project demonstrates an active release cadence with regular maintenance updates and significant version bumps, such as v4.0.0. It is frequently used for setting up development environments, testing database queries, and establishing initial application states.
npm install mongo-seeding-cliVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically seed a MongoDB database using the `mongo-seeding` library in TypeScript, including configuring the seeder, reading data from files, and applying transformers. It also highlights the usage of `bulkWriteOptions` introduced in v4.0.0.
Update your `SeederConfig` to use `bulkWriteOptions` instead of `collectionInsertManyOptions`. For example, `{ bulkWriteOptions: { ordered: false } }`.Ensure your Node.js environment meets the minimum requirements specified in the package's `package.json` or release notes. Consider using `nvm` or a similar tool to manage Node.js versions.
If encountering issues with TypeScript data files, add `ts-node` to your project and register it, or use the `--transpile-only` flag with the CLI: `DEBUG=mongo-seeding npx ts-node your-seed-script.ts` or `seed --transpile-only ./data`.
Replace `collectionInsertManyOptions` with `bulkWriteOptions` in your `SeederConfig` object.
For programmatic use, ensure your project is configured for ESM (add `"type": "module"` to `package.json`) and use `import` statements. If using CommonJS, stick to `require()` and ensure the library version supports it, or use a transpiler.
Verify that your MongoDB instance is running and accessible. Check the connection URI/details (host, port, credentials) in your `SeederConfig` or CLI parameters (e.g., `--db-uri`). Ensure no firewall is blocking the connection. Run `docker run --rm -p 27017:27017 mongo` for a quick local MongoDB instance.
Double-check the `path.resolve()` argument. Ensure your data directory has subdirectories named after your collections (e.g., `./data/users/user-data.js`) and each file exports an array or object of documents.