Samhammer.Mongo
6.0.3
dotnet add package Samhammer.Mongo --version 6.0.3
NuGet\Install-Package Samhammer.Mongo -Version 6.0.3
<PackageReference Include="Samhammer.Mongo" Version="6.0.3" />
paket add Samhammer.Mongo --version 6.0.3
#r "nuget: Samhammer.Mongo, 6.0.3"
// Install Samhammer.Mongo as a Cake Addin
#addin nuget:?package=Samhammer.Mongo&version=6.0.3
// Install Samhammer.Mongo as a Cake Tool
#tool nuget:?package=Samhammer.Mongo&version=6.0.3
Samhammer.Mongo
Usage
This package provides access to mongodb over the mongodb driver. It includes basic access functionality to the database and provides a possibility to define models.
How to add this to your project:
- reference this package to your main project: https://www.nuget.org/packages/Samhammer.Mongo/
- reference this package to your model project: https://www.nuget.org/packages/Samhammer.Mongo.Abstractions/
- initialize mongodb in Startup.cs
- add the health check to Startup.cs (optional)
- add the mongodb configuration to the appsettings (if the lib is initialized with IConfiguration in Startup.cs)
Example Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMongoDb(Configuration); // Init by configuration or action
services.AddHealthChecks()
.AddMongoDb();
}
Example appsettings configuration:
"MongoDbOptions": {
"UserName": "dbuser",
"Password": "dbpassword",
"DatabaseName": "dbname",
"AuthDatabaseName": "admin", // defaults to the database name
"DatabaseHost": "dbhost.tld",
"ConnectionString": "mongodb://dbhost.tld" // alternative to DatabaseHost
},
Example model
Model for a collection with the name "user".
[MongoCollection]
public class UserModel : BaseModelMongo
{
public string LoginName { get; set; }
}
Example repository
The base repo provides the following actions:
- Task<T> GetById(string id);
- Task<List<T>> GetAll();
- Task Save(T model);
- Task Delete(T model);
- Task DeleteAll();
Here is an example with the additional method GetByLoginName:
public class UserRepositoryMongo : BaseRepositoryMongo<UserModel>, IUserRepositoryMongo
{
public UserRepositoryMongo(ILogger<UserRepositoryMongo> logger, IMongoDbConnector connector)
: base(logger, connector)
{
}
public async Task<UserModel> GetByLoginName(string loginName)
{
var entries = await Collection.FindAsync(i => i.LoginName == loginName);
return entries.FirstOrDefault();
}
}
public interface IUserRepositoryMongo : IBaseRepositoryMongo<UserModel>
{
Task<UserModel> GetByLoginName(string loginName);
}
Query Logging
It is possible to log the plain queries, by enabling trace logging for Samhammer.Mongo. Here is an example with Serilog configured in appsettings.json
"Serilog": {
"MinimumLevel": {
"Override": {
"Samhammer.Mongo": "Verbose"
}
}
},
Here is an example how the log message will look like:
mongodb command: {
"find" : "label",
"filter" : { "ProjectId" : ObjectId("63bfb836e9103fd33216f6b8") },
"$db" : "mytestdb",
"lsid" : { "id" : CSUUID("c95ac0da-18d3-4fda-ac27-fe8e9eb0e034") }
}
Contribute
How to publish package
- create git tag
- The nuget package will be published automatically by a github action
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- AspNetCore.HealthChecks.MongoDb (>= 6.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
- MongoDB.Driver (>= 2.14.1)
- Samhammer.Mongo.Abstractions (>= 6.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.