使用 MongoDb 处理迁移

Handling migrations with MongoDb(使用 MongoDb 处理迁移)

本文介绍了使用 MongoDb 处理迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了给这个问题提供更多背景信息,我有一个 Web 应用程序 (asp mvc),它基本上将 CRUD 操作包装到 MongoDb 实例中,它在模型被验证并发送到之前执行验证和某些业务逻辑存储、检索等.

Just to give a little more context to the question, I have a web application (asp mvc) which basically wraps CRUD operations to a MongoDb instance, it carries out validation and certain business logic before the model is verified and sent over to be stored, retrieved etc.

现在我们遇到的一个问题是,在新版本中模型已经改变,但现有数据没有,这里有一个例子:(它是 c# 特定的,但问题确实与语言无关)

Now one problem we have his is that in the new version the models have changed but the existing data has not, here is an example: (it is c# specific but the question really is language agnostic)

public class Person
{
    public Guid Id {get; set;}
    public string Name {get; set;}
    public int Age {get;set;}
    public string BadgeNo {get;set;}
}

public class Person
{
    public Guid Id {get; set;}
    public string Name {get; set;}
    public int Age {get;set;}
    public string EmployeeNo {get; set;} // Still contains same data as BadgeNo just called something different
}

您可以看到对象的结构发生了变化,但在 Mongo 领域,它仍然传递一个 BadgeNo,而不是一个 EmployeeNo.在 SQL 领域,我们通常会有一个迁移脚本,它作为构建脚本的一部分运行,它会更改架构并更新/插入/删除该增量的任何其他数据.

As you can see the structure of the objects have changed but in Mongo land it is still passing out a BadgeNo, not an EmployeeNo. In an SQL land we would usually have a migration script which is run as part of the build script which would change the schema and update/insert/delete any additional data for that delta.

那么如何最好地使用 Mongo 管理这些类型的迁移?我是否还应该有一个脚本用于更新 Mongo 中的所有实例?还是有其他一些首选的做法来做这种事情.

So how is it best to manage these sort of migrations with Mongo? Should I also have a script which I use to update all instances within Mongo? or is there some other preferred practise for doing this sort of thing.

任何关于这个主题的建议都会很棒

Any advice on the subject would be great

=== 编辑 ===

目前我似乎想要使用迁移选项而不是逐步淘汰的方法,因此考虑到这一点,任何人都可以推荐任何工具来帮助该领域,否则每次迁移(假设滚入,roll-out) 必须是某种预编译的程序集,其中包含所有逻辑.我在想一些类似于 FluentMigrator 的东西,但不是使用 SQL,而是使用 Mongo.目前我的构建脚本使用的是 Nant,我看过一些 ruby​​ 工具,但不确定是否有任何 .net 等价物.

It seems like currently I am wanting to go with the migration option rather than a phasing out approach, so with this in mind can anyone recommend any tools for helping in this area, as otherwise each migration (assuming a roll-in, roll-out) would have to be a pre compiled assembly of some kind with all the logic in. I was thinking something along the lines of FluentMigrator but instead of working with SQL you are working with Mongo. Currently my build scripts are using Nant, I have seen some ruby tools but not sure if there are any .net equivalent.

推荐答案

基本上有两种方法:

  1. 确保您的应用程序代码可以处理数据结构的两个版本",并在保存时更新到新结构
  2. 编写迁移脚本

我可能会选择选项 1,因为它是允许您逐步更新的方法,而与选项 2 一样,您基本上需要关闭您的应用程序,以便您可以更新代码(快速)和数据(可能更慢)一口气.

I would probably go for option 1 as it's the method that allows you to gradually update, where as with option 2 you basically need to take down your application so that you can update the code (fast) and data (possibly slower) in one go.

然后,或者如果您认为有必要也执行选项 2 来迁移您的数据.这样就不必关闭您的网站,并且可以愉快地在后台异步运行.

Then later, or if you find it necessary do option 2 as well to migrate your data over. This then doesn't have to take down your site, and can happily run asynchronously in the background.

这篇关于使用 MongoDb 处理迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 MongoDb 处理迁移

基础教程推荐