{"id":29007198,"url":"https://github.com/vladimirrybalko/csharpmongomigrations","last_synced_at":"2025-06-25T13:07:35.464Z","repository":{"id":46223745,"uuid":"62280229","full_name":"VladimirRybalko/CSharpMongoMigrations","owner":"VladimirRybalko","description":"CSharp migrations tool for MongoDb","archived":false,"fork":false,"pushed_at":"2025-06-17T13:47:36.000Z","size":78,"stargazers_count":28,"open_issues_count":1,"forks_count":23,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-23T12:05:04.429Z","etag":null,"topics":["csharp","dotnet","migrations","mongo-migrator","mongodb","mongodb-driver","netstandard20"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VladimirRybalko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-30T04:50:18.000Z","updated_at":"2025-06-17T13:47:41.000Z","dependencies_parsed_at":"2024-12-12T15:20:38.405Z","dependency_job_id":"4017dc7c-3615-46ad-90fb-b5095318cf16","html_url":"https://github.com/VladimirRybalko/CSharpMongoMigrations","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/VladimirRybalko/CSharpMongoMigrations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VladimirRybalko%2FCSharpMongoMigrations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VladimirRybalko%2FCSharpMongoMigrations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VladimirRybalko%2FCSharpMongoMigrations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VladimirRybalko%2FCSharpMongoMigrations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VladimirRybalko","download_url":"https://codeload.github.com/VladimirRybalko/CSharpMongoMigrations/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VladimirRybalko%2FCSharpMongoMigrations/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261879323,"owners_count":23223739,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","migrations","mongo-migrator","mongodb","mongodb-driver","netstandard20"],"created_at":"2025-06-25T13:07:34.761Z","updated_at":"2025-06-25T13:07:35.449Z","avatar_url":"https://github.com/VladimirRybalko.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSharpMongoMigrations\n\n[![NuGet](https://img.shields.io/badge/nuget-2.5.1-blue.svg)](https://www.nuget.org/packages/CSharpMongoMigrations/)\n\n## What is it?\n\n**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.\n\n\n## Documentation API\nGenerally, there are four types of migrations.\n\n1) **Common migration**\n\n```csharp\n   [Migration(0, \"Add John Doe\")]\n    public class AddPersonMigration : Migration\n    {\n        public override void Up()\n        {\n            var collection = GetCollection(\"Persons\");\n            var document = new BsonDocument();\n\n            document.AddUniqueIdentifier(new Guid(\"06BFFCF5-DAE9-422A-85AB-F58DE41E86DA\"));\n            document.AddProperty(\"Name\", \"John Doe\");\n            \n            collection.InsertOne(document);\n        }\n\n        public override void Down()\n        {\n            var collection = GetCollection(\"Persons\");\n            var idFilter = Builders\u003cBsonDocument\u003e.Filter.Eq(\"_id\", new Guid(\"06BFFCF5-DAE9-422A-85AB-F58DE41E86DA\"));\n            collection.DeleteOne(idFilter);\n        }\n    }\n```\n\nHere 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.\n\nPay your attention to the **Migration** attribute. It's required for running migration by the launcher. You should define the unique migration number (\u003cspan style=\"color:gray\"\u003e'0' in example\u003c/span\u003e) and arbitrary description (\u003cspan style=\"color:gray\"\u003e'Add John Doe'\u003c/span\u003e).\n\n\n2) **Document migration**\n\nThese migrations allow to apply changes to each document in the specified collection.\n```csharp\n   [Migration(1, \"Change persons\")]\n    public class AddPropertyPersonMigration : DocumentMigration\n    {\n        protected override string CollectionName { get { return \"Persons\"; } }\n               \n        protected override void UpgradeDocument(BsonDocument document)\n        {\n            document.AddProperty(\"IsActive\", true);            \n        }\n\n        protected override void DowngradeDocument(BsonDocument document)\n        {\n            document.RemoveProperty(\"IsActive\");\n        }\n    }\n```\n\n\n3) **Collection migration**\n\nThese migrations allow to apply changes to each collection separately from another ones.\n```csharp\n   [Migration(\"Animals\", 0)]\n    public sealed class AddAnimalMigration : Migration\n    {\n        public override void Up()\n        {\n            var collection = GetCollection(\"Animals\");\n            var document = new BsonDocument();\n\n            document.AddUniqueIdentifier(new Guid(\"2A7B73A8-3C4A-422D-90B4-C73BCF48EBD4\"));\n            document.AddProperty(\"Kind\", \"Cat\");\n\n            collection.InsertOne(document);\n        }\n\n        public override void Down()\n        {\n            var collection = GetCollection(\"Animals\");\n            var idFilter = Builders\u003cBsonDocument\u003e.Filter.Eq(\"_id\", new Guid(\"2A7B73A8-3C4A-422D-90B4-C73BCF48EBD4\"));\n            collection.DeleteOne(idFilter);\n        }\n    }\n```\nHere, 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.\n\n4) **Conditional migration**\n\nThese migrations might be skipped based on defined condition.\n```csharp\n    [Migration(4, \"Add Migration when condition meets\")]\n    public sealed class ConditionalMigrations : Migration\n    {\n        public override void Up()\n        {\n            var collection = GetCollection(\"Persons\");\n            var document = new BsonDocument();\n\n            document.AddUniqueIdentifier(new Guid(\"20C2CAC4-C55D-4C5C-8937-33698A3EC6C7\"));\n            document.AddProperty(\"Name\", \"John Doe - Conditional\");\n\n            collection.InsertOne(document);\n        }\n\n        public override void Down()\n        {\n            var collection = GetCollection(\"Persons\");\n            var idFilter = Builders\u003cBsonDocument\u003e.Filter.Eq(\"_id\", new Guid(\"20C2CAC4-C55D-4C5C-8937-33698A3EC6C7\"));\n            collection.DeleteOne(idFilter);\n        }\n\n        // Up condition\n        public override bool ShouldUp()\n        {\n            return true;\n        }\n\n        // Down condition\n        public override bool ShouldDown()\n        {\n            return false;\n        }\n    }\n```\n\nYou can also find more detailed examples in the *CSharpMongoMigrations.Demo* project.\n\n\n## How to launch migrations?\nIt's actually simple. You need to create an instance of the *MigrationRunner* class.\n\n```csharp\n   var runner = new MigrationRunner(\"\u003cmongoDb_connection_string\u003e\", \"\u003cdatabase_name\u003e\", \"\u003cfull_name_of_assembly_with_migrations\u003e\");\n```\nThen you can call the *Up* or *Down* method to apply or downgrade migrations.\n\n```csharp\n   // Apply all migrations before specified version.\n   // Use -1 as a version parameter to apply all existing migrations. ('-1' is a default parameter value)\n   runner.Up(\"\u003cversion\u003e\"); \n   \n   // Roll back all migrations after specified version.\n   // Use -1 as a version parameter to downgrade all existing migrations. ('-1' is a default parameter value)\n   runner.Down(\"\u003cversion\u003e\"); \n```\n\nIt's worth noting that above methods execute all types of migrations. To launch the collection specific migrations, please use one of the polymorphic methods.\n```csharp\n   // Apply all migrations before specified version.\n   // Use -1 as a version parameter to apply all existing migrations for the target collection. ('-1' is a default parameter value)\n   runner.Up(\"\u003cCollection_name\u003e\", \"\u003cversion\u003e\"); \n   \n   // Roll back all migrations after specified version.\n   // Use -1 as a version parameter to downgrade all existing migrations for the target collection. ('-1' is a default parameter value)\n   runner.Down(\"\u003cCollection_name\u003e\", \"\u003cversion\u003e\"); \n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvladimirrybalko%2Fcsharpmongomigrations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvladimirrybalko%2Fcsharpmongomigrations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvladimirrybalko%2Fcsharpmongomigrations/lists"}