如何正确处理猫鼬架构迁移? [英] How to properly handle mongoose schema migrations?

查看:63
本文介绍了如何正确处理猫鼬架构迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB&的新手.猫鼬,似乎找不到解决方案更改时如何处理迁移的答案.

I'm completely new to MongoDB & Mongoose and can't seem to find an answer as to how to handle migrations when a schema changes.

我习惯于运行迁移SQL脚本,这些脚本会更改表结构和任何需要更改的基础数据.这通常涉及数据库停机.

I'm used to running migration SQL scripts that alter table structure and any underlying data that needs to be changed. This typically involves DB downtime.

在MongoDB/Mongoose中通常如何处理?我需要知道的任何陷阱吗?

How is this typically handled within MongoDB/Mongoose? Any gotcha's that I need to be aware of?

推荐答案

在了解这一点并合理地了解迁移如何在关系数据库上工作时,MongoDB使其变得更加简单.我有两种方法可以解决这个问题.在MongoDB中处理数据迁移时要考虑的事情(并非所有RDB都不太常见):

In coming across this and reasonably understanding how migrations work on a relational database, MongoDB makes this a little simpler. I've come to 2 ways to break this down. The things to consider when dealing with data migrations in MongoDB (not all that uncommon from RDBs) are:

  • 当开发人员合并项目存储库中的最新版本时,确保本地测试环境不会中断
  • 确保无论使用登录还是注销用户(如果使用身份验证),都可以在实时版本上正确更新任何数据. (当然,如果升级时每个人都自动注销,那么只需要担心用户何时登录).

1)如果您的更改将使所有人注销,或者预计应用程序将停机,则执行此操作的简单方法是使用迁移脚本来连接到本地或实时MongoDB并升级正确的数据.将用户名从单个字符串更改为具有给定名称和姓氏的对象的示例(当然,这是非常基本的,需要将其放入脚本中才能为所有开发人员运行):

1) If your change will log everyone out or application downtime is expected then the simple way to do this is have a migration script to connect to local or live MongoDB and upgrade the correct data. Example where a user's name is changed from a single string to an object with given and family name (very basic of course and would need to be put into a script to run for all developers):

使用CLI:

mongod
use myDatabase
db.myUsers.find().forEach( function(user){
    var curName = user.name.split(' '); //need some more checks..

    user.name = {given: curName[0], family: curName[1]};
    db.myUsers.save( user );
})

2)您希望应用程序根据其正在运行的应用程序版本上下迁移架构.显然,这对于实时服务器而言将减轻负担,并且由于仅在用户首次使用升级/降级版本时升级用户,因此无需停机.

2) You want the application to migrate the schemas up and down based on the application version they are running. This will obviously be less of a burden for a live server and not require down time due to only upgrading users when they use the upgraded / downgraded versions for the first time.

如果您在Expressjs for Nodejs中使用中间件:

If your using middleware in Expressjs for Nodejs:

  • 通过app.set('schemaVersion', 1)在根应用程序脚本中设置一个应用程序变量,该变量将在以后与用户架构版本进行比较.
  • 现在确保所有用户架构也都具有schemaVersion属性,以便我们仅可以针对特定用户"检测应用程序架构版本和当前MongoDB架构之间的更改.
  • 接下来,我们需要创建简单的中间件来检测配置和用户版本

  • Set an app variable in your root app script via app.set('schemaVersion', 1) which will be used later to compare to the users schema version.
  • Now ensure all the user schemas have a schemaVersion property as well so we can detect a change between the application schema version and the current MongoDB schemas for THAT PARTICULAR USER only.
  • Next we need to create simple middleware to detect the config and user version

app.use( function( req, res, next ){
  //If were not on an authenticated route
  if( ! req.user ){
    next();
    return;
  }
  //retrieving the user info will be server dependent
  if( req.user.schemaVersion === app.get('schemaVersion')){
    next();
    return;
  }

  //handle upgrade if user version is less than app version

  //handle downgrade if user version is greater than app version

  //save the user version to your session / auth token / MongoDB where necessary
})

对于升级/降级,我将在带有迁移/升级的导出功能的迁移目录下制作简单的js文件,该功能将接受用户模型并在MongoDB中对该特定用户运行迁移更改.最后,确保用户版本在您的MongoDB中已更新,这样他们就不会再次运行更改,除非他们再次移至另一个版本.

For the upgrade / downgrade I would make simple js files under a migrations directory with an upgrade / downgrade export functions that will accept the user model and run the migration changes on that particular user in the MongoDB. Lastly ensure the users version is updated in your MongoDB so they don't run the changes again unless they move to a different version again.

这篇关于如何正确处理猫鼬架构迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆