mongo-seeding is a Node.js library specifically designed to populate MongoDB databases with initial data, serving critical roles in development, testing, and creating demo environments. It offers flexibility in data definition, supporting various formats like JSON files and custom JavaScript/TypeScript scripts, and provides granular control over the data insertion process. The library is currently in its active development phase, with version 4.0.2 being the latest stable release as of recent updates. Its release cadence typically involves regular maintenance updates addressing dependencies and minor bug fixes, alongside significant major version increments that introduce breaking changes and new features. A key differentiator for `mongo-seeding` is its declarative approach to defining seed data, comprehensive support for both programmatic API usage and a command-line interface (CLI), and robust error handling mechanisms during data insertion, which collectively make it a reliable and efficient solution for managing and initializing MongoDB database states.
npm install mongo-seedingVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `mongo-seeding`, configure the database connection, read seed data from a directory, and import it into MongoDB. It includes handling potential errors during the seeding process.
Update your `SeederConfig` to use `bulkWriteOptions` instead of `collectionInsertManyOptions`. For example, `bulkWriteOptions: { ordered: true }` replaces `collectionInsertManyOptions: { ordered: true }`.Verify that your seed data files (e.g., `users.js`) contain a `export default [...]` where `...` is an array of MongoDB documents.
Ensure you manage the MongoDB client lifecycle externally if necessary. If `mongo-seeding` creates the client internally, there might not be a direct method to close it via the `Seeder` instance. Consider explicitly connecting and passing an `MongoClient` instance to the seeder's config if fine-grained control is needed.
Ensure your MongoDB instance is running. Verify the `database.uri` in your `SeederConfig` or the `MONGO_URI` environment variable is correct and points to an active MongoDB server.
Double-check the path provided to `readCollectionsFromPath`. Use `path.resolve(__dirname, 'your-data-directory')` to ensure an absolute path is used, especially when running from different working directories.
Ensure the `mongodb` driver is installed and compatible with `mongo-seeding`'s version. Check the `database.uri` and `database.options` in your `SeederConfig` for correct connection parameters.