CSharpMongoMigrations 2.5.2

dotnet add package CSharpMongoMigrations --version 2.5.2
                    
NuGet\Install-Package CSharpMongoMigrations -Version 2.5.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharpMongoMigrations" Version="2.5.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpMongoMigrations" Version="2.5.2" />
                    
Directory.Packages.props
<PackageReference Include="CSharpMongoMigrations" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharpMongoMigrations --version 2.5.2
                    
#r "nuget: CSharpMongoMigrations, 2.5.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=CSharpMongoMigrations&version=2.5.2
                    
Install as a Cake Addin
#tool nuget:?package=CSharpMongoMigrations&version=2.5.2
                    
Install as a Cake Tool

CSharpMongoMigrations

NuGet

What is it?

CSharpMongoMigrations is an alternative .NET library for MongoDB migrations. Unlike the official package, it supports both Up and Down migrations, which can be a significant advantage for large-scale MongoDB projects. In addition, it has no dependencies on outdated MongoDB drivers, so there's no need to manage multiple driver versions anymore.

Documentation API

Generally, there are four main types of migrations:

  1. 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);
        }
    }

In this example, we define the Up migration to add John Doe to the Person collection. The Down method rolls back the migration, restoring the database to its original state. Take note of the Migration attribute — it's required for the migration to be executed by the launcher. You must specify a unique migration number (e.g., <span style="color:gray">'0'</span>) and provide a descriptive label (e.g., <span style="color:gray">'Add John Doe'</span>).

  1. 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");
        }
    }
  1. 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 specifies the target collection name and the migration version for a predefined collection. This enables migrations to be applied to a specific schema.

  1. Conditional migration

These migrations can be conditionally skipped based on predefined criteria.

    [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;
        }
    }

For more in-depth examples, check out the CSharpMongoMigrations.Demo project.

How to launch migrations?

It’s actually quite simple: just create an instance of the MigrationRunner class.

   var runner = new MigrationRunner("<mongoDb_connection_string>", "<database_name>", "<full_name_of_assembly_with_migrations>");

You can then call the Up or Down method to apply or revert 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>"); 

By default, these methods execute all defined migration types. To run migrations for a specific collection, use one of the available polymorphic overloads.

   // 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 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.2 128 7/1/2025
2.5.1 1,820 2/12/2025
2.5.0 18,308 10/14/2024
2.4.0 24,660 6/22/2023
2.3.0 22,639 11/5/2021
2.2.0 37,788 1/7/2020
2.1.2 18,051 5/29/2019
2.1.1 2,976 1/8/2019
2.1.0 14,305 7/17/2018
2.0.0 40,598 12/29/2017
1.1.0 2,105 11/25/2016
1.0.3 1,619 7/1/2016
1.0.2 1,567 7/1/2016
1.0.1 1,578 7/1/2016
1.0.0 2,015 6/30/2016

MongoDBDriver is upgraded.