Tools.Net.Mongo.Core
1.0.0-alpha
See the version list below for details.
dotnet add package Tools.Net.Mongo.Core --version 1.0.0-alpha
NuGet\Install-Package Tools.Net.Mongo.Core -Version 1.0.0-alpha
<PackageReference Include="Tools.Net.Mongo.Core" Version="1.0.0-alpha" />
<PackageVersion Include="Tools.Net.Mongo.Core" Version="1.0.0-alpha" />
<PackageReference Include="Tools.Net.Mongo.Core" />
paket add Tools.Net.Mongo.Core --version 1.0.0-alpha
#r "nuget: Tools.Net.Mongo.Core, 1.0.0-alpha"
#:package Tools.Net.Mongo.Core@1.0.0-alpha
#addin nuget:?package=Tools.Net.Mongo.Core&version=1.0.0-alpha&prerelease
#tool nuget:?package=Tools.Net.Mongo.Core&version=1.0.0-alpha&prerelease
.NET Mongo
Overview
A Global Tool for the dotnet CLI to manage MongoDB databases in .NET.
Installation
This package contains a .NET Core Global Tool you can call from the shell/command line. To install use the following command:
dotnet tool install --global Tools.Net.Mongo
To specify the specific version, use the --version
tag.
dotnet tool install --global Tools.Net.Mongo --version 1.0.0
CLI Usage
Tools:
--migrate <COMMAND> Manages MongoDB migrations
Commands:
create <NAME> Creates a new migration file. NAME is required to create a migration
up [OPTIONS] Runs all migrations that have not been applied
down [OPTIONS] Downgrades the database by undoing the last applied migration
status [OPTIONS] Prints the changelog of the database
Options:
-i|--uri The MongoDB connection string
Getting Started
Start by creating a .NET Core or .NET Standard project. Once the project in created, change your shell/command line directory to the directory of the project file.
Next, install the Tools.Net.Mongo.Core package in your project. *Note: The dotnet Mongo tool uses the Mongo Core package for its migrations. If you're receiving build errors after creating a migration, verify the MOngo Core package is installed properly.
Migrations
.NET Mongo Migrations provides functionality to manage the state of a MongoDB instance using the dotnet mongo migrate
command.
Creating a New Migration
To create a new migration use the dotnet mongo --migrate create <NAME>
command. A migration file will be created in the Migrations directory of the project.
PS C:\Repositories\demo\Tools.Mongo.Demo> dotnet mongo --migrate create MigrationDemo
Created: Migrations/M201908311227533_MigrationDemo.cs
After the migration is created, implement the Up and Down functions in the generated migration file.
public bool Up(IMongoDatabase database)
{
var usersCollection = database.GetCollection<BsonDocument>("users");
usersCollection.InsertOne(new BsonDocument
{
{"firstName", "Rick"},
{"lastName", "Grimes"},
{"email", "rgrimes@twd.com"},
});
var filterDefinition = Builders<BsonDocument>.Filter.Eq("firstName", "Rick");
filterDefinition = filterDefinition & Builders<BsonDocument>.Filter.Eq("lastName", "Grimes");
filterDefinition = filterDefinition & Builders<BsonDocument>.Filter.Eq("email", "rgrimes@twd.com");
var newUser = usersCollection.Find(filterDefinition).ToList();
if (newUser.Count == 1) return true;
return false;
}
public bool Down(IMongoDatabase database)
{
var usersCollection = database.GetCollection<BsonDocument>("users");
var filterDefinition = Builders<BsonDocument>.Filter.Eq("firstName", "Rick");
filterDefinition = filterDefinition & Builders<BsonDocument>.Filter.Eq("lastName", "Grimes");
filterDefinition = filterDefinition & Builders<BsonDocument>.Filter.Eq("email", "rgrimes@twd.com");
var result = usersCollection.DeleteOne(filterDefinition);
if (result.DeletedCount == 1) return true;
return false;
}
Note: Each function returns a bool value to indicate if the script was run successfully
Upgrading a Database
To upgrate a database, run the dotnet mongo --migrate up --url <connectionString>
command. The up
command will run all migrations that have not been applied to a given database instance. Note: This command must be executed in the project directory where the Migrations folder lives.
PS C:\Repositories\demo\Tools.Mongo.Demo> dotnet mongo --migrate up --uri mongodb://localhost:27017/twdDb
Migrated: M201908311227533_MigrationDemo
Downgrading a Database
To downgrade a database, run the dotnet mongo --migrate down --url <connectionString>
command. The down
command will undo the latest migration from a given database. Note: This command must be executed in the project directory where the Migrations folder lives.
PS C:\Repositories\demo\Tools.Mongo.Demo> dotnet mongo --migrate down --uri mongodb://localhost:27017/twdDb
Downgraded: M201908311227533_MigrationDemo
Checking the Migration Status
To see the status of a database instance, run the dotnet mongo --migrate status --url <connectionString>
command. Note: This command must be executed in the project directory where the Migrations folder lives.
PS C:\Repositories\demo\Tools.Mongo.Demo> dotnet mongo --migrate status --uri mongodb://localhost:27017/twdDb
+--------------------------------+------------+
| Migration | Applied At |
+--------------------------------+------------+
| M201908311227533_MigrationDemo | PENDING |
+--------------------------------+------------+
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. 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 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. 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. |
-
.NETStandard 2.0
- MongoDB.Driver (>= 2.9.1)
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 |
---|---|---|
3.4.0 | 160 | 1/4/2025 |
3.3.0 | 311 | 10/7/2023 |
3.2.0 | 192 | 5/13/2023 |
3.1.0 | 619 | 4/29/2022 |
3.0.0 | 629 | 4/14/2022 |
2.0.0 | 700 | 1/22/2022 |
2.0.0-rc | 469 | 12/24/2019 |
2.0.0-beta | 462 | 12/23/2019 |
1.0.0 | 662 | 9/13/2019 |
1.0.0-alpha | 505 | 8/31/2019 |
A library for MongoDB databases in .NET