CSharpMongoMigrations 2.5.0
dotnet add package CSharpMongoMigrations --version 2.5.0
NuGet\Install-Package CSharpMongoMigrations -Version 2.5.0
<PackageReference Include="CSharpMongoMigrations" Version="2.5.0" />
paket add CSharpMongoMigrations --version 2.5.0
#r "nuget: CSharpMongoMigrations, 2.5.0"
// Install CSharpMongoMigrations as a Cake Addin #addin nuget:?package=CSharpMongoMigrations&version=2.5.0 // Install CSharpMongoMigrations as a Cake Tool #tool nuget:?package=CSharpMongoMigrations&version=2.5.0
CSharpMongoMigrations
What is it?
CSharpMongoMigrations is an alternative .NET library for mongo migrations. Despite the official package the package allows the both Up and Down migrations. It might be a big advantage for the huge MongoDb projects. Moreover, the solution does not have any dependencies from the outdated MongoDb driver. So you don't need to reference different driver versions anymore.
Documentation API
Generally, there are four types of migrations.
- Common migration
[Migration(0, "Add John Doe")]
public class AddPersonMigration : Migration
{
public override void Up()
{
var collection = GetCollection("Persons");
var document = new BsonDocument();
document.AddUniqueIdentifier(new Guid("06BFFCF5-DAE9-422A-85AB-F58DE41E86DA"));
document.AddProperty("Name", "John Doe");
collection.InsertOne(document);
}
public override void Down()
{
var collection = GetCollection("Persons");
var idFilter = Builders<BsonDocument>.Filter.Eq("_id", new Guid("06BFFCF5-DAE9-422A-85AB-F58DE41E86DA"));
collection.DeleteOne(idFilter);
}
}
Here we define the Up migration to added John Doe to Person collection. The Down method roll back the migration and restore database to the original state.
Pay your attention to the Migration attribute. It's required for running migration by the launcher. You should define the unique migration number (<span style="color:gray">'0' in example</span>) and arbitrary description (<span style="color:gray">'Add John Doe'</span>).
- Document migration
These migrations allow to apply changes to each document in the specified collection.
[Migration(1, "Change persons")]
public class AddPropertyPersonMigration : DocumentMigration
{
protected override string CollectionName { get { return "Persons"; } }
protected override void UpgradeDocument(BsonDocument document)
{
document.AddProperty("IsActive", true);
}
protected override void DowngradeDocument(BsonDocument document)
{
document.RemoveProperty("IsActive");
}
}
- Collection migration
These migrations allow to apply changes to each collection separately from another ones.
[Migration("Animals", 0)]
public sealed class AddAnimalMigration : Migration
{
public override void Up()
{
var collection = GetCollection("Animals");
var document = new BsonDocument();
document.AddUniqueIdentifier(new Guid("2A7B73A8-3C4A-422D-90B4-C73BCF48EBD4"));
document.AddProperty("Kind", "Cat");
collection.InsertOne(document);
}
public override void Down()
{
var collection = GetCollection("Animals");
var idFilter = Builders<BsonDocument>.Filter.Eq("_id", new Guid("2A7B73A8-3C4A-422D-90B4-C73BCF48EBD4"));
collection.DeleteOne(idFilter);
}
}
Here, the Migration attribute defines the target collection name and the specific migration version for the predefined collection. It helps us to apply migration to a specific schema.
- Conditional migration
These migrations might be skipped based on defined condition.
[Migration(4, "Add Migration when condition meets")]
public sealed class ConditionalMigrations : Migration
{
public override void Up()
{
var collection = GetCollection("Persons");
var document = new BsonDocument();
document.AddUniqueIdentifier(new Guid("20C2CAC4-C55D-4C5C-8937-33698A3EC6C7"));
document.AddProperty("Name", "John Doe - Conditional");
collection.InsertOne(document);
}
public override void Down()
{
var collection = GetCollection("Persons");
var idFilter = Builders<BsonDocument>.Filter.Eq("_id", new Guid("20C2CAC4-C55D-4C5C-8937-33698A3EC6C7"));
collection.DeleteOne(idFilter);
}
// Up condition
public override bool ShouldUp()
{
return true;
}
// Down condition
public override bool ShouldDown()
{
return false;
}
}
You can also find more detailed examples in the CSharpMongoMigrations.Demo project.
How to launch migrations?
It's actually simple. You need to create an instance of the MigrationRunner class.
var runner = new MigrationRunner("<mongoDb_connection_string>", "<database_name>", "<full_name_of_assembly_with_migrations>");
Then you can call the Up or Down method to apply or downgrade migrations.
// Apply all migrations before specified version.
// Use -1 as a version parameter to apply all existing migrations. ('-1' is a default parameter value)
runner.Up("<version>");
// Roll back all migrations after specified version.
// Use -1 as a version parameter to downgrade all existing migrations. ('-1' is a default parameter value)
runner.Down("<version>");
It's worth noting that above methods execute all types of migrations. To launch the collection specific migrations, please use one of the polymorphic methods.
// Apply all migrations before specified version.
// Use -1 as a version parameter to apply all existing migrations for the target collection. ('-1' is a default parameter value)
runner.Up("<Collection_name>", "<version>");
// Roll back all migrations after specified version.
// Use -1 as a version parameter to downgrade all existing migrations for the target collection. ('-1' is a default parameter value)
runner.Down("<Collection_name>", "<version>");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- MongoDB.Driver (>= 2.20.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.5.0 | 3,802 | 10/14/2024 |
2.4.0 | 18,804 | 6/22/2023 |
2.3.0 | 21,006 | 11/5/2021 |
2.2.0 | 36,598 | 1/7/2020 |
2.1.2 | 17,972 | 5/29/2019 |
2.1.1 | 2,897 | 1/8/2019 |
2.1.0 | 14,067 | 7/17/2018 |
2.0.0 | 38,669 | 12/29/2017 |
1.1.0 | 1,933 | 11/25/2016 |
1.0.3 | 1,447 | 7/1/2016 |
1.0.2 | 1,396 | 7/1/2016 |
1.0.1 | 1,405 | 7/1/2016 |
1.0.0 | 1,654 | 6/30/2016 |
Added conditional migrations. Project is migrated to the .netstandard 2.1.