- In MongoDB, migrations refer to processes used to modify the structure or format of data in your database.
- This is the forward migration that applies changes to the database to achieve a desired state.
- Examples include adding new fields, creating indexes, renaming collections, or transforming data.
- It’s typically used to implement new features or adapt to updated application requirements.
- This is the rollback migration that undoes the changes made by the up migration.
- Examples include removing fields, reverting indexes, or restoring data to its original format.
- It’s useful for reverting the database to its previous state if something goes wrong.
- BackEnd : NodeJS, Express
- Database : Mongodb
- NPM Package : migrate-mongo
-
Clone and navigate to this project directory
-
Install the dependencies
npm install
-
Create sample env file and change configuration according to your need in ".env" file and create Database
-
Run the node/express server
npm run start
-
Open the url
http://localhost:<PORT>
in browser for seeing the result -
Initialize Migrations:
migrate-mongo init
-
Create a Migration File:
migrate-mongo create add-new-field
-
Edit the Migration File: Open the migration file and define the up and down operations.
-
Run the up Migration:
npx migrate-mongo up
-
Rollback (Run the down Migration):
npx migrate-mongo down