mongoose-seeder

raw JSON →
1.2.1 verified Sat Apr 25 auth: no javascript abandoned

Seed MongoDB databases with JSON data using Mongoose models. Version 1.2.1 allows seeding from JSON files, automatically resolving model references. Supports promises and callbacks. Options control whether to drop the entire database, drop only seeded collections, or append data. Last updated 2015; no active maintenance. Simpler than faker-based seeders but lacks advanced features like auto-increment or relationships.

error TypeError: seeder.seed is not a function
cause Incorrect import (default export not assigned).
fix
Use 'const seeder = require('mongoose-seeder')' (no .default).
error Error: No model found for 'User'
cause Mongoose model not registered or typo in _model value.
fix
Ensure 'User' model is defined with mongoose.model('User', schema) before seeding.
error MongooseError: Can't create model inside a query
cause Models defined after seeding attempt.
fix
Define all models before calling seed().
deprecated Package has not been updated since 2015; use at your own risk.
fix Consider alternatives like mongodb-seeding or mongoose-dummy-seeder.
gotcha The data object uses string keys (e.g., 'foo') for document fields; these become property keys in the result, not just IDs.
fix Use meaningful keys; they are preserved in the returned dbData object.
gotcha Requires Mongoose models to be defined and connected before seeding.
fix Call mongoose.connect() and ensure models are registered before calling seeder.seed().
breaking All data is dropped by default (dropDatabase: true).
fix Set options { dropDatabase: false } or { dropCollections: true } to preserve existing data.
npm install mongoose-seeder
yarn add mongoose-seeder
pnpm add mongoose-seeder

Seeds a MongoDB database with a User document from JSON data, dropping the entire database first.

const seeder = require('mongoose-seeder');
const data = {
  users: {
    _model: 'User',
    alice: { firstName: 'Alice', name: 'Smith', email: 'alice@example.com' }
  }
};
seeder.seed(data, { dropDatabase: true })
  .then(dbData => console.log('Seeded:', dbData))
  .catch(err => console.error(err));