Skip to content

Instantly share code, notes, and snippets.

@pkosiec
Last active August 2, 2021 11:43
Show Gist options
  • Save pkosiec/27070f5b716b968028227c64685e87ea to your computer and use it in GitHub Desktop.
Save pkosiec/27070f5b716b968028227c64685e87ea to your computer and use it in GitHub Desktop.
Seed MongoDB database programmatically
const path = require('path');
const { Seeder } = require('mongo-seeding');
const config = {
database: {
name: 'mydbname',
},
dropDatabase: true,
};
const seeder = new Seeder(config);
const collections = seeder.readCollectionsFromPath(
path.resolve('./mydb/data-import'),
{
transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],
},
);
seeder
.import(collections)
.then(() => {
console.log('Success');
})
.catch(err => {
console.log('Error', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment