我有一个与 mongoDb 一起使用的 .NET Core 项目。 我研究了数据迁移,但没有一种解决方案适合我。
有 MongoMigrations NuGet 包,但它与 .NET Core 不兼容。 目标是有一些方法可以检查当前的数据库版本,如果它不是最新的,则进行某些更新,例如调用某些RemoveUserCollection()方法或类似的方法。
有人遇到同样的问题,或者有更好的解决方案吗? :)
谢谢!
最佳答案
我自己也曾多次偶然发现这个问题,但手动将其烘焙到应用程序启动中或仅使用 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(),
});
}
)
}
})();
关于.NET Core 的 MongoDB 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47075733/