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!!!

No Comments Yet