Sagar Acharya
Sagar Acharya

Reputation: 1871

How to create migrations in moongoose?

I am using node js and i previously used to work using knex in my applications. In knex we create migrations to define the structure of the tables initially.

Is there something like this in mongoose to create schemas and seed initial data?

Upvotes: 1

Views: 95

Answers (1)

Nandun Bandara
Nandun Bandara

Reputation: 174

There are two strategies:

  • Write a script that will take care of upgrading the schema as well as downgrading it to previous versions
  • Update your documents as they are used

The second one is much more code-dependent and must stay in your codebase. If the code is somehow removed, then many of documents are not upgradeable.

For instance, if there have been 3 versions of a document, [1, 2, and 3] and we remove the upgrade code from version 1 to version 2, any documents that still exist as version 1 are not upgradeable. I personally see this as overhead to maintaining code and it becomes inflexible.

I hope the following reference link would help you out.

Link

Upvotes: 1

Related Questions