Tharga.MongoDb
1.9.27
See the version list below for details.
dotnet add package Tharga.MongoDb --version 1.9.27
NuGet\Install-Package Tharga.MongoDb -Version 1.9.27
<PackageReference Include="Tharga.MongoDb" Version="1.9.27" />
paket add Tharga.MongoDb --version 1.9.27
#r "nuget: Tharga.MongoDb, 1.9.27"
// Install Tharga.MongoDb as a Cake Addin #addin nuget:?package=Tharga.MongoDb&version=1.9.27 // Install Tharga.MongoDb as a Cake Tool #tool nuget:?package=Tharga.MongoDb&version=1.9.27
Tharga.MongoDb
This package was broken out of a product that needs the possibility of dynamic naming of databases and collections. I also helps with structuring what functions are to be accessed for different types.
It also have some aditional features for Atlas MongoDB like limiting the size of the responses and opening the firewall.
Get started
Install the nuget package Tharga.MongoDb
.
Register to use
Register this package at startup by calling AddMongoDb
as an extension to IServiceCollection
.
public void ConfigureServices(IServiceCollection services)
{
services.AddMongoDb();
}
By default the configuration setting ConnectionStrings:Default
is used to get the connection string.
Customize by providing DatabaseOptions
to AddMongoDb
.
Create entities, repositories and collections.
The simplest way is to have the repository implement the collection directly.
public class MySimpleRepo : DiskRepositoryCollectionBase<MyEntity, ObjectId>
{
public MySimpleRepo(IMongoDbServiceFactory mongoDbServiceFactory)
: base(mongoDbServiceFactory)
{
}
}
public record MyEntity : EntityBase<ObjectId>
{
}
The more complex way that gives more control is to implement one class for the repo and another for the collection. This way you can control what methods repo methods are exposed to consumers. Here implemented with interfaces and the collection made internal.
public interface IMySimpleRepo : IRepository
{
public Task<MyEntity> GetFirstOrDefaultAsync();
}
public class MySimpleRepo : IMySimpleRepo
{
private readonly IMySimpleCollection _mySimpleCollection;
public MySimpleRepo(IMySimpleCollection mySimpleCollection)
{
_mySimpleCollection = mySimpleCollection;
}
public Task<MyEntity> GetFirstOrDefaultAsync()
{
return _mySimpleCollection.GetOneAsync(x => true);
}
}
public interface IMySimpleCollection : IRepositoryCollection<MyEntity, ObjectId>
{
}
internal class MySimpleCollection : DiskRepositoryCollectionBase<MyEntity, ObjectId>, IMySimpleCollection
{
public MySimpleCollection(IMongoDbServiceFactory mongoDbServiceFactory)
: base(mongoDbServiceFactory)
{
}
public Task<MyEntity> GetFirstOrDefaultAsync()
{
throw new NotImplementedException();
}
}
public record MyEntity : EntityBase<ObjectId>
{
}
Simple Console Sample
This is a simple demo for a console application written in .NET 7. The following nuget packages are used.
- Tharga.MongoDb
- Microsoft.Extensions.Hosting
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson;
using Tharga.MongoDb;
using Tharga.MongoDb.Disk;
var services = new ServiceCollection();
services.AddMongoDb(o => o.ConnectionStringLoader = _ => "mongodb://localhost:27017/SimpleConsoleSample");
var serviceProvider = services.BuildServiceProvider();
var simpleRepo = serviceProvider.GetService<MySimpleRepo>();
await simpleRepo!.AddAsync(new MyEntity());
var oneItem = await simpleRepo.GetOneAsync(x => true);
Console.WriteLine($"Got item with id '{oneItem.Id}' from the database.");
public class MySimpleRepo : DiskRepositoryCollectionBase<MyEntity, ObjectId>
{
public MySimpleRepo(IMongoDbServiceFactory mongoDbServiceFactory)
: base(mongoDbServiceFactory)
{
}
}
public record MyEntity : EntityBase<ObjectId>
{
}
More details
Configuration
Configuring can be done in appsettings.json
or by code. Code is always used first value by value.
If using multiple (named) databases, configuration will always use the named version first if there is one and then use the general fallback value.
This is the order used, value by value.
- Named configuration from code
- General configuration from code
- Named configuration from IConfiguration
- General configuration from IConfiguration
- Default values
Example of configuration by appsettings.json
.
When the 'Default' database is used, the result limit will be 100, for the 'Other' database the result limit will be 200. If another database is implemented, the fallback of 1000 will be used as result limit.
The 'Default' database will have the firewall opened, if hosted in Atlas MongoDB.
"ConnectionStrings": {
"Default": "mongodb://localhost:27017/Tharga{environment}_Sample{part}",
"Other": "mongodb://localhost:27017/Tharga{environment}_Sample_Other{part}"
},
"MongoDb": {
"Default": {
"AccessInfo": {
"PublicKey": "[PublicKey]",
"PrivateKey": "[PrivateKey]",
"ClusterId": "[ClusterId]"
},
"ResultLimit": 100,
"AutoClean": true,
"CleanOnStartup": true,
"DropEmptyCollections": true
},
"Other": {
"ResultLimit": 200
},
"ResultLimit": 1000
"AutoClean": false,
"CleanOnStartup": false,
"DropEmptyCollections": false
}
Example of configuration by code.
This would be the same configuration as from the example above.
services.AddMongoDb(o =>
{
o.ConnectionStringLoader = name =>
{
return (string)name switch
{
"Default" => "mongodb://localhost:27017/Tharga{environment}_Sample{part}",
"Other" => "mongodb://localhost:27017/Tharga{environment}_Sample_Other{part}",
_ => throw new ArgumentException($"Unknown configuration name '{name}'.")
};
};
o.Configuration = new MongoDbConfigurationTree
{
Configurations = new Dictionary<ConfigurationName, MongoDbConfiguration>
{
{
"Default", new MongoDbConfiguration
{
AccessInfo = new MongoDbApiAccess
{
PublicKey = "[PublicKey]",
PrivateKey = "[PrivateKey]",
ClusterId = "[ClusterId]"
},
ResultLimit = 100,
AutoClean = true,
CleanOnStartup = true,
DropEmptyCollections = true
}
},
{
"Other", new MongoDbConfiguration
{
ResultLimit = 200
}
}
},
ResultLimit = 1000,
AutoClean = false,
CleanOnStartup = false,
DropEmptyCollections = false
};
});
Customize collections
Properties for classes deriving from RepositoryCollectionBase<,>
can be customised directly by overriding the default behaviour of the code or configuration.
By default the name of the collection is the same as the type name of the entity.
To have a different name the property CollectionName
can be overridden.
The name of the database can be built up dynamically, use DatabasePart
to do so.
Read more about this in the section MongoUrl Builder.
Override property ConfigurationName
to use different database than default (or set as default in DatabaseOptions
).
This makes it possible to use multiple databases from the same application.
The properties AutoClean
, CleanOnStartup
, DropEmptyCollections
and ResultLimit
can be overridden by collection to be different from the configuration.
To automatically register known types when using multiple types in the same collection, provide a value for Types
.
Create Indicies
by overriding the property in your collection class.
The list of Indicies
is applied befor the first record is added to the collection.
It is also reviewed once every time the application starts, removing Indicies
that no longer exists and creates new ones if the code have changed.
MongoUrl Builder
The MongoUrl
is created by a built in implementation of IMongoUrlBuilder
. It takes the raw version and parses variables to build MongoUrl
.
Two variables are supported {environment}
and {part}
.
To dynamicaly change the name of the database {part}
can be used. It can be used as an override to a collection or provided as a variable in DatabaseContext
together with CollectionProvider.
For {environment}
the value will be ommitted when it is set to 'Production'.
Both variables will get a leading character of '_'.
Example for Development with the databasePart = MyPart.
mongodb://localhost:27017/Tharga{environment}_Sample{part}
--> mongodb://localhost:27017/Tharga_Development_Sample_MyPart
Custom MongoUrl Builder
If there is a need for a custom string builder, implement the interface IMongoUrlBuilder
and register with the IOC and that will be used instead of the built in version.
Register your own version of IMongoUrlBuilder in IOC.
services.AddTransient<IMongoUrlBuilder, MyMongoUrlBuilder>();
MongoDB Result Limit
It is possible to se t a hard limit for the number of documents returned. If the limit is reached ResultLimitException
is thrown.
For large result-sets, use the method GetPageAsync
to get the ResultLimit
on each page of the result.
{
"MongoDb": {
"ResultLimit": 500
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net7.0
- Microsoft.Extensions.Configuration.Binder (>= 7.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- MongoDB.Driver (>= 2.20.0)
- System.Linq.Async (>= 6.0.1)
- Tharga.Toolkit (>= 1.9.27)
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.14.2 | 87 | 12/12/2024 |
1.13.3 | 144 | 12/9/2024 |
1.13.2 | 131 | 11/8/2024 |
1.13.1 | 123 | 10/24/2024 |
1.12.17 | 447 | 10/22/2024 |
1.12.9 | 179 | 7/25/2024 |
1.12.4 | 155 | 7/21/2024 |
1.12.3 | 114 | 7/21/2024 |
1.12.2 | 139 | 7/7/2024 |
1.12.1 | 186 | 6/24/2024 |
1.11.14 | 298 | 5/2/2024 |
1.11.12 | 158 | 4/14/2024 |
1.11.11 | 113 | 4/14/2024 |
1.11.10 | 259 | 3/2/2024 |
1.11.9 | 216 | 2/26/2024 |
1.11.8 | 279 | 2/9/2024 |
1.11.7 | 278 | 2/5/2024 |
1.11.6 | 315 | 2/4/2024 |
1.11.5 | 410 | 1/9/2024 |
1.11.4 | 369 | 1/9/2024 |
1.11.2 | 469 | 12/16/2023 |
1.11.1 | 502 | 12/3/2023 |
1.10.39 | 469 | 11/21/2023 |
1.10.38 | 528 | 10/14/2023 |
1.10.37 | 549 | 10/1/2023 |
1.10.36 | 537 | 10/1/2023 |
1.10.35 | 518 | 10/1/2023 |
1.10.34 | 524 | 9/30/2023 |
1.10.33 | 513 | 9/30/2023 |
1.10.30 | 509 | 9/28/2023 |
1.10.25 | 559 | 9/25/2023 |
1.10.24 | 530 | 9/23/2023 |
1.10.23 | 541 | 9/23/2023 |
1.10.19 | 597 | 8/27/2023 |
1.10.14 | 698 | 7/31/2023 |
1.10.13 | 638 | 7/26/2023 |
1.10.12 | 647 | 7/25/2023 |
1.10.11 | 650 | 7/20/2023 |
1.10.10 | 678 | 7/19/2023 |
1.10.9 | 648 | 7/18/2023 |
1.10.7 | 636 | 7/17/2023 |
1.10.6 | 676 | 7/14/2023 |
1.10.3 | 651 | 7/14/2023 |
1.10.2 | 663 | 7/12/2023 |
1.9.27 | 636 | 7/10/2023 |
1.9.26 | 624 | 7/10/2023 |
1.9.25 | 596 | 7/10/2023 |
1.9.19 | 651 | 7/7/2023 |
1.9.18 | 597 | 7/6/2023 |
1.9.17 | 639 | 7/4/2023 |
1.9.13 | 649 | 7/3/2023 |
1.9.12 | 606 | 7/3/2023 |
1.9.11 | 639 | 5/20/2023 |
1.9.10 | 637 | 5/19/2023 |
1.9.9 | 644 | 5/18/2023 |
1.9.8 | 630 | 5/18/2023 |
1.9.7 | 658 | 5/18/2023 |
1.9.6 | 651 | 5/18/2023 |
1.9.5 | 614 | 5/18/2023 |
1.9.4 | 661 | 5/16/2023 |
1.9.3 | 599 | 5/15/2023 |
1.9.2 | 628 | 5/14/2023 |
1.8.19 | 569 | 5/11/2023 |
1.8.17 | 673 | 4/5/2023 |
1.8.16 | 727 | 3/23/2023 |
1.8.15 | 773 | 12/23/2022 |
1.8.13 | 801 | 12/11/2022 |
1.8.12 | 769 | 12/9/2022 |
1.8.11 | 759 | 11/28/2022 |
1.8.10 | 767 | 11/28/2022 |
1.8.9 | 820 | 11/12/2022 |
1.8.8 | 867 | 11/3/2022 |
1.8.7 | 837 | 11/1/2022 |
1.8.6 | 831 | 11/1/2022 |
1.8.5 | 788 | 11/1/2022 |
1.8.4 | 831 | 10/30/2022 |
1.8.3 | 801 | 10/30/2022 |
1.7.10 | 857 | 9/20/2022 |
1.7.5 | 794 | 11/29/2021 |
1.7.4 | 793 | 11/28/2021 |
1.6.203 | 851 | 9/9/2021 |
1.6.202 | 781 | 9/8/2021 |
1.6.192 | 827 | 7/15/2021 |
1.6.167 | 829 | 5/23/2021 |
1.6.166 | 803 | 5/23/2021 |
1.6.164 | 808 | 5/23/2021 |
1.6.163 | 923 | 3/13/2021 |
1.6.159 | 853 | 2/21/2021 |
1.6.157 | 841 | 2/19/2021 |
1.6.156 | 824 | 2/19/2021 |
1.6.153 | 809 | 2/18/2021 |
1.0.127 | 851 | 1/16/2021 |