【问题标题】:MongoDB migrations for .NET Core.NET Core 的 MongoDB 迁移
【发布时间】:2018-04-15 00:04:06
【问题描述】:

我有一个与 mongoDb 配合使用的 .NET Core 项目。 我进行了有关数据迁移的研究,但没有一种解决方案适合我。

有 MongoMigrations NuGet 包,但它与 .NET Core 不兼容。 目标是有一些方法可以检查当前的数据库版本,如果它不是最新的,则进行某些更新,例如调用一些 RemoveUserCollection() 方法或类似的方法。

有没有人遇到过同样的问题,或者有更好的解决方案? :)

谢谢!

【问题讨论】:

标签: mongodb asp.net-core .net-core database-migration


【解决方案1】:

我自己也曾多次偶然发现这个问题,但手动将其烘焙到您的应用程序启动中或仅使用 javascript 编写脚本并像任何其他系统更改一样部署它需要花费 2 分钟。

我有一个类似的 PowerShell 脚本

param([Parameter(Mandatory=$True)][string]$Hostname, [string]$Username = $null, [string]$Password = $null)

if($Username){
    $authParameters = "-u $Username -p $Password --authenticationDatabase admin"
}

Write-Host "Running all migration scripts..."
$scripts = Get-Childitem | where {$_.extension -like '.js'} | foreach { $_.Name }
Invoke-Expression "c:\mongodb\bin\mongo.exe --host $Hostname $authParameters $scripts"

然后我只是将大量 .js 文件转储到同一目录中。

;
(function () {
    var migration = { _id: ObjectId("568b9e75e1e6530a2f4d8884"), name: "Somekinda Migration (929394)" };

    var testDb = db.getMongo().getDB('test');
    if (!testDb.migrations.count({ _id: migration._id })) {
        testDb.migrations.insert(migration);
        print('running ' + migration.name)

        // Do Work
        testDb.people.find().snapshot().forEach(
            function (alm) {
                testDb.people.save(
                    {
                        modifiedAt: new Date(),
                    });
            }
        )
    }
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多