Dealing with schema changes in Mongoose

后端 未结 3 1434
轻奢々
轻奢々 2020-11-30 22:37

What\'s the best practice (or tool) for updating/migrating Mongoose schemas as the application evolves?

3条回答
  •  不知归路
    2020-11-30 22:51

    Update: Tested, this does not work in its current form, its got the right idea going, I got a single migration to work with considerable tweaking of the module itself. But I don't see it working as intended without some major changes and keeping track of the different schemas somehow.


    Sounds like you want mongoose-data-migrations

    It is intended to migrate old schema versions of your documents as you use them, which is seems to be the best way to handle migrations in mongodb.

    You don't really want to run full dataset migrations on a document collection (ala alter table) as it puts a heavy load on your servers and could require application / server downtime. Sometimes you may need to write a script which simply grabs all documents applies the new schema / alterations and calls save, but you need to understand when/where to do that. An example might be, adding migration logic to doc init has more of a performance hit than taking down the server for 3 hours to run migration scripts is worth.

    I found this link pretty helpful as well, basically reiterates the above in more detail and essentially implements the above node package's concept in php.

    N.B. The module is 5months old, 0 forks, but am looking around and can't find anything better / more helpful than abdelsaid's style of response..

提交回复
热议问题