DEV Community

Adeeyo adewale temitayo
Adeeyo adewale temitayo

Posted on

Seed MongoDB (mongoose-seed)

Firstly What is database seeding?

Seeding a database is a process in which an initial set of data is provided to a database when it is being installed. It is especially useful when we want to populate the database with data we want to develop in future.

Now that we know what database seeding is let write some code,I believe you are familiar with Nodejs and MongoDB, so set up your Nodejs server. then create a product.model.js file and write the following:

Screen Shot 2019-10-25 at 11.39.48 PM.png

Create product.controller.js file and write the following to create a product:

Screen Shot 2019-10-25 at 11.48.39 PM.png

Now create a product.route.js file and write the following:

Screen Shot 2019-10-25 at 11.52.22 PM.png

Now let create our seed.js file and write the following :

Screen Shot 2019-10-26 at 12.08.09 AM.png

Screen Shot 2019-10-26 at 12.09.19 AM.png

In your package.json add a seed script:

Screen Shot 2019-10-26 at 12.11.52 AM.png
now run

npm run seed

to seed data to MongoDB

to read more on mongoose-seed

Enjoy!!!

Top comments (3)

Collapse
 
arnabsen1729 profile image
Arnab Sen

Great Article. Just a suggestion instead of giving pictures of code snippets, it becomes helpful if you add the code using markdown.

Collapse
 
dev_tycodez profile image
Adeeyo adewale temitayo

Alright man, thanks for the feed back

Collapse
 
aayushojha profile image
Ayush Ojha

I think providing _id manually is not a good idea.