Samhammer.Mongo
8.0.0
dotnet add package Samhammer.Mongo --version 8.0.0
NuGet\Install-Package Samhammer.Mongo -Version 8.0.0
<PackageReference Include="Samhammer.Mongo" Version="8.0.0" />
paket add Samhammer.Mongo --version 8.0.0
#r "nuget: Samhammer.Mongo, 8.0.0"
// Install Samhammer.Mongo as a Cake Addin #addin nuget:?package=Samhammer.Mongo&version=8.0.0 // Install Samhammer.Mongo as a Cake Tool #tool nuget:?package=Samhammer.Mongo&version=8.0.0
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. The package also supports multiple connections from different servers.
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 Program.cs
- add the health check to Program.cs (optional)
- add the mongodb configuration to the appsettings (if the lib is initialized with IConfiguration in Program.cs)
Example Program.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMongoDb(Configuration); // Init by configuration or action
services.AddHealthChecks()
.AddMongoDb();
}
For multiple credentials:
public void ConfigureServices(IServiceCollection services)
{
services.AddMongoDb(Configuration); // Init by configuration or action
services.AddHealthChecks()
.AddMongoDb(credentials);
}
Example appsettings configuration:
"MongoDbOptions": {
"DatabaseCredentials": [
{
"UserName": "dbuser",
"Password": "dbpassword",
"DatabaseName": "dbname",
"AuthDatabaseName": "admin", // defaults to the database name
"DatabaseHost": "dbhost.tld",
"ConnectionString": "mongodb://dbhost.tld" // alternative to DatabaseHost
},
{
"UserName": "dbuser2",
"Password": "dbpassword2",
"DatabaseName": "dbname2",
"AuthDatabaseName": "admin", // defaults to the database name
"DatabaseHost": "dbhost2.tld",
"ConnectionString": "mongodb://dbhost2.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. If the database name is not provided, the library will use the first set of DatabaseCredentials in appsetting:
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);
}
To specify which credential should be used:
public class UserRepositoryMongo : BaseRepositoryMongo<UserModel>, IUserRepositoryMongo
{
public UserRepositoryMongo(ILogger<UserRepositoryMongo> logger, IMongoDbConnector connector)
: base(logger, connector, "dbName")
{
}
}
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 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. |
.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
- AspNetCore.HealthChecks.MongoDb (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- MongoDB.Driver (>= 2.24.0)
- Samhammer.Mongo.Abstractions (>= 8.0.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 |
---|---|---|
8.0.0 | 1,995 | 3/7/2024 |
6.1.1 | 1,701 | 8/14/2023 |
6.1.0 | 162 | 8/14/2023 |
6.0.5 | 3,317 | 4/3/2023 |
6.0.4 | 335 | 3/28/2023 |
6.0.3 | 1,176 | 1/18/2023 |
6.0.2 | 280 | 1/18/2023 |
6.0.1 | 1,352 | 9/15/2022 |
6.0.0 | 4,165 | 1/20/2022 |
1.3.0 | 490 | 12/20/2021 |
1.2.0 | 350 | 12/6/2021 |
1.1.0 | 465 | 10/21/2021 |
1.0.0 | 438 | 8/26/2021 |