This .NET Tool provides some utilities that can be useful during a project's development phase when working with MongoDB and can do the following:
- Dropping a DB
- Checking the health of a DB
- Running a MongoDBMigrations migration
- Saving/Loading scenario data
As a testing aid, you can save the current state of the DB as a scenario JSON file and load it back in again. This is useful for quickly testing against various data states and sharing data states with the team.
⚠ Warning: This is not intended as a way to back up / restore large amounts of data but rather manage various light-weight scenarios for a small amount of data. The scenario commands will attempt to dump / restore the entire DB using an inefficient JSON file format.
This is a work in progress and has not had any production exposure but I thought I would share it as it has been useful for me.
This utility uses the excellent MongoDBMigrations project - https://bitbucket.org/i_am_a_kernel/mongodbmigrations
Mongo Utils (this): 0.0.10
MongoDBMigrations: 2.2.0
MongoDB.Driver: 2.24.0
Release Notes: https://github.com/Discofunk/BlueGoat.MongoDBUtils/releases
Currently dependencies are packaged with this utility at a specific version which may cause issues if you are using an older MongoDBMigrations version or your MongoDB database is not compatible with the current driver being used.
Added option to select MongoDB GuidRepresentationMode
and GuidRepresentation
when loading & saving scenario data to maintain expected format after loading. For more information see https://www.mongodb.com/docs/drivers/csharp/current/fundamentals/serialization/guid-serialization/
Examples:
mongo-utils scenario load -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" --in .\ModelData\BasicScenario.json --guid-mode V3
To install the Mongo Utils tool run globally:
dotnet tool install --global BlueGoat.MongoUtils
Verify it is installed by running mongo-utils -?
To install the Mongo Utils locally for a given project:
dotnet new tool-manifest #creates a tool manifest that can be shared with the team
dotnet tool install --local BlueGoat.MongoUtils --version 0.0.3
Verify install by running dotnet mongo-utils -?
Note: Local installations must prefix with the
dotnet
command to run
Mongo Utils comes with help:
mongo-utils -h
Description:
MongoDB Utilities
Usage:
mongo-utils [command] [options]
Options:
-c, --connection <connection> (REQUIRED) MongoDB connection String
--version Show version information
-?, -h, --help Show help and usage information
Commands:
migrate Run MongoDB migration
drop Drop a MongoDB database
reset Drop database and run all migrations
health Runs a Health Check against a MongoDB instance
scenario Scenario Commands
mongo-utils migrate -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" -ma .\MongoDb.Migrations.dll
mongo-utils migrate -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" -ma .\MongoDb.Migrations.dll --version 1.0.2
mongo-utils drop -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB"
mongo-utils reset -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" -ma .\MongoDb.Migrations.dll
mongo-utils health -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB"
mongo-utils scenario save -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" --out .\ModelData\BasicScenario.json
mongo-utils scenario load -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" --in .\ModelData\BasicScenario.json
mongo-utils scenario save -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" --out .\ModelData\BasicScenario.json --guid-mode V3
mongo-utils scenario load -c mongodb://root:BlueGoatsFlyFaster@localhost:27017 -db "MyMongoDB" --in .\ModelData\BasicScenario.json --guid-mode V3