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 | 53 | 12/12/2024 |
1.13.3 | 138 | 12/9/2024 |
1.13.2 | 128 | 11/8/2024 |
1.13.1 | 121 | 10/24/2024 |
1.12.17 | 445 | 10/22/2024 |
1.12.9 | 177 | 7/25/2024 |
1.12.4 | 151 | 7/21/2024 |
1.12.3 | 112 | 7/21/2024 |
1.12.2 | 136 | 7/7/2024 |
1.12.1 | 184 | 6/24/2024 |
1.11.14 | 296 | 5/2/2024 |
1.11.12 | 156 | 4/14/2024 |
1.11.11 | 111 | 4/14/2024 |
1.11.10 | 257 | 3/2/2024 |
1.11.9 | 212 | 2/26/2024 |
1.11.8 | 277 | 2/9/2024 |
1.11.7 | 276 | 2/5/2024 |
1.11.6 | 313 | 2/4/2024 |
1.11.5 | 408 | 1/9/2024 |
1.11.4 | 367 | 1/9/2024 |
1.11.2 | 467 | 12/16/2023 |
1.11.1 | 500 | 12/3/2023 |
1.10.39 | 467 | 11/21/2023 |
1.10.38 | 526 | 10/14/2023 |
1.10.37 | 547 | 10/1/2023 |
1.10.36 | 535 | 10/1/2023 |
1.10.35 | 516 | 10/1/2023 |
1.10.34 | 522 | 9/30/2023 |
1.10.33 | 511 | 9/30/2023 |
1.10.30 | 507 | 9/28/2023 |
1.10.25 | 557 | 9/25/2023 |
1.10.24 | 528 | 9/23/2023 |
1.10.23 | 539 | 9/23/2023 |
1.10.19 | 595 | 8/27/2023 |
1.10.14 | 697 | 7/31/2023 |
1.10.13 | 637 | 7/26/2023 |
1.10.12 | 646 | 7/25/2023 |
1.10.11 | 649 | 7/20/2023 |
1.10.10 | 677 | 7/19/2023 |
1.10.9 | 647 | 7/18/2023 |
1.10.7 | 635 | 7/17/2023 |
1.10.6 | 675 | 7/14/2023 |
1.10.3 | 650 | 7/14/2023 |
1.10.2 | 662 | 7/12/2023 |
1.9.27 | 635 | 7/10/2023 |
1.9.26 | 623 | 7/10/2023 |
1.9.25 | 595 | 7/10/2023 |
1.9.19 | 650 | 7/7/2023 |
1.9.18 | 596 | 7/6/2023 |
1.9.17 | 638 | 7/4/2023 |
1.9.13 | 648 | 7/3/2023 |
1.9.12 | 605 | 7/3/2023 |
1.9.11 | 638 | 5/20/2023 |
1.9.10 | 636 | 5/19/2023 |
1.9.9 | 643 | 5/18/2023 |
1.9.8 | 629 | 5/18/2023 |
1.9.7 | 657 | 5/18/2023 |
1.9.6 | 650 | 5/18/2023 |
1.9.5 | 613 | 5/18/2023 |
1.9.4 | 660 | 5/16/2023 |
1.9.3 | 598 | 5/15/2023 |
1.9.2 | 627 | 5/14/2023 |
1.8.19 | 568 | 5/11/2023 |
1.8.17 | 671 | 4/5/2023 |
1.8.16 | 726 | 3/23/2023 |
1.8.15 | 772 | 12/23/2022 |
1.8.13 | 800 | 12/11/2022 |
1.8.12 | 768 | 12/9/2022 |
1.8.11 | 756 | 11/28/2022 |
1.8.10 | 764 | 11/28/2022 |
1.8.9 | 817 | 11/12/2022 |
1.8.8 | 864 | 11/3/2022 |
1.8.7 | 834 | 11/1/2022 |
1.8.6 | 828 | 11/1/2022 |
1.8.5 | 785 | 11/1/2022 |
1.8.4 | 828 | 10/30/2022 |
1.8.3 | 798 | 10/30/2022 |
1.7.10 | 853 | 9/20/2022 |
1.7.5 | 791 | 11/29/2021 |
1.7.4 | 790 | 11/28/2021 |
1.6.203 | 848 | 9/9/2021 |
1.6.202 | 778 | 9/8/2021 |
1.6.192 | 824 | 7/15/2021 |
1.6.167 | 826 | 5/23/2021 |
1.6.166 | 800 | 5/23/2021 |
1.6.164 | 805 | 5/23/2021 |
1.6.163 | 920 | 3/13/2021 |
1.6.159 | 850 | 2/21/2021 |
1.6.157 | 838 | 2/19/2021 |
1.6.156 | 821 | 2/19/2021 |
1.6.153 | 806 | 2/18/2021 |
1.0.127 | 848 | 1/16/2021 |