csharp-mongodb-migrations
1.2.0
See the version list below for details.
dotnet add package csharp-mongodb-migrations --version 1.2.0
NuGet\Install-Package csharp-mongodb-migrations -Version 1.2.0
<PackageReference Include="csharp-mongodb-migrations" Version="1.2.0" />
<PackageVersion Include="csharp-mongodb-migrations" Version="1.2.0" />
<PackageReference Include="csharp-mongodb-migrations" />
paket add csharp-mongodb-migrations --version 1.2.0
#r "nuget: csharp-mongodb-migrations, 1.2.0"
#:package csharp-mongodb-migrations@1.2.0
#addin nuget:?package=csharp-mongodb-migrations&version=1.2.0
#tool nuget:?package=csharp-mongodb-migrations&version=1.2.0
MongoDB Migrations
Simple database migrations for MongoDB.Driver using ASP.NET DI.
Quick start
Install the NuGet package & Implement the database migrations system in your solution using the following steps:
dotnet add package csharp-mongodb-migrations
Inject migrations service
Inject AddMigrations into your services.
- If migrations are not in the
Assembly.GetEntryAssembly, then manually specify the assemblies in the parameters ofAddMigrations. - Call
AddMigrationsafter configuring options.
using MongoDB.Migration;
services
.Configure<MyDatabaseSettings>(builder.Configuration.GetSection("Database:MyDatabaseSettings"))
.AddMigrations()
Update settings
Implement IMongoMigratable for your database settings.
- Options must be configured before injecting services.
- A
Database.Aliasmust be unique: The name used to identify the database during runtime. - A
Database.Namemust be unique: The name of the actual MongoDB database. - Assign a unique constant value to
MigrationDescriptor.Database.Alias.
public sealed record MyDatabaseSettings : IOptions<MyDatabaseSettings>, IMongoMigratable
{
public required string ConnectionString { get; init; }
public required string DatabaseName { get; init; }
public required string MyCollectionName { get; init; }
MyDatabaseSettings IOptions<MyDatabaseSettings>.Value => this;
public MongoMigrableDefinition GetMigratableDefinition()
{
return new()
{
ConnectionString = ConnectionString,
Database = new("MyDatabase", DatabaseName),
MirgrationStateCollectionName = "DATABASE_MIGRATIONS"
};
}
}
Await migrations
Wait for migrations to complete using IMongoMigrationCompletion before accessing the database at any point.
WaitAsyncmay not ever be called in constructors.WaitAsyncis fast when the migration is completed, or the database is not configured.
sealed class MyRepository(IOptions<MyDatabaseSettings> databaseSettings, IMongoMigrationCompletion migrationCompletion) {
private readonly IMongoCollection<MyModel> _myCollection = new MongoClient(databaseSettings.ConnectionString)
.GetDatabase(databaseSettings.DatabaseName)
.GetCollection<MyModel>(databaseSettings.MyCollectionName);
public async Task<MyModel> GetOrSetAsync(MyModel insertModel, CancellationToken cancellationToken = default) {
var migrated = await migrationCompletion.WaitAsync(databaseSettings.Value, cancellationToken).ConfigureAwait(false);
// ... do stuff
}
}
Implement migrations
Add IMongoMigrations between version 0 and 1 and so on.
- The alias used for the
MongoMigrationAttributemust match the name inIMongoMigratable.GetMigrationSettings().Database.Alias.
[MongoMigration("MyDatabase", 0, 1, Description = "Add composite index to MyCollection")]
public sealed class MyCollectionAddCompositeIndexMigration(IOptions<MyDatabaseSettings> optionsAccessor) : IMongoMigration
{
public async Task DownAsync(IMongoDatabase database, CancellationToken cancellationToken = default)
{
}
public async Task UpAsync(IMongoDatabase database, CancellationToken cancellationToken = default)
{
}
}
Fixing database versions
A fixed database version ensures that the migration always produces a database of the specified version, even if the initial version was higher than the fixed version.
- Fixing upgrades the database, if the initial version is lower than the fixed version.
- Fixing downgrades the database, if the initial version is higher than the fixed version.
- Fixing the database to a version is especially useful for a procedural feature rollout.
Fix a specific version by setting the long? MigrateToFixedVersion property in your IMongoMigratable; if the property is null - by default - a migration to the latest version will occur.
public MongoMigrableDefinition GetMigratableDefinition()
{
return new()
{
ConnectionString = ConnectionString,
Database = new("MyDatabase", DatabaseName),
MirgrationStateCollectionName = "DATABASE_MIGRATIONS",
MigrateToFixedVersion = 42069,
};
}
Migrations without ASP.NET DI
MongoDB Migration extensively uses ASP.NET to prepare the environment required for processing of migrations. Without ASP.NET most features are unavailable, but the core - DatabaseMirationProcessor - public API; it is accessible to the user.
- Describe your database in the
MongoMigrableDefinitionrecord. - Wrap instances of all available
IMongoMigrations in theMigrationDescriptorrecord. - Instantiate
DatabaseMirationProcessorfor your database. - Call
MigrateToVersionAsyncwith the availableMigrationDescriptors.
Testing
Tests require a local mongodb instance without authentication at port 27017: mongodb://localhost:27017.
Disclaimer
csharp-mongo-migration is not affiliated with or endorsed by MongoDB, Inc. MONGODB & MONGO are registered trademarks of MongoDB, Inc. 2023.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.2
- csharp-mongodb-migrations-core (>= 1.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- MongoDB.Driver (>= 2.22.0)
- System.Collections.Immutable (>= 7.0.0)
-
.NETStandard 2.0
- csharp-mongodb-migrations-core (>= 1.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- MongoDB.Driver (>= 2.22.0)
- System.Collections.Immutable (>= 7.0.0)
-
.NETStandard 2.1
- csharp-mongodb-migrations-core (>= 1.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- MongoDB.Driver (>= 2.22.0)
- System.Collections.Immutable (>= 7.0.0)
-
net6.0
- csharp-mongodb-migrations-core (>= 1.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- MongoDB.Driver (>= 2.22.0)
-
net7.0
- csharp-mongodb-migrations-core (>= 1.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- MongoDB.Driver (>= 2.22.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 |
|---|---|---|
| 1.3.1-preview.0.2 | 47 | 11/11/2025 |
| 1.2.0 | 275 | 12/21/2023 |
| 1.1.1 | 152 | 11/5/2023 |
| 1.1.0 | 103 | 11/4/2023 |
| 1.0.0 | 74 | 11/3/2023 |
| 0.1.4 | 91 | 11/3/2023 |
| 0.1.3-preview.0.8 | 69 | 11/3/2023 |
| 0.0.0-preview.0.32 | 60 | 11/3/2023 |
| 0.0.0-preview.0.28 | 75 | 11/3/2023 |
| 0.0.0-preview.0.16 | 64 | 11/3/2023 |