Mongo.GenericClient
8.2.0
See the version list below for details.
dotnet add package Mongo.GenericClient --version 8.2.0
NuGet\Install-Package Mongo.GenericClient -Version 8.2.0
<PackageReference Include="Mongo.GenericClient" Version="8.2.0" />
<PackageVersion Include="Mongo.GenericClient" Version="8.2.0" />
<PackageReference Include="Mongo.GenericClient" />
paket add Mongo.GenericClient --version 8.2.0
#r "nuget: Mongo.GenericClient, 8.2.0"
#:package Mongo.GenericClient@8.2.0
#addin nuget:?package=Mongo.GenericClient&version=8.2.0
#tool nuget:?package=Mongo.GenericClient&version=8.2.0
Mongo.GenericClient
Generic library to manage a MongoDB database.
Required environment variables
"MONGODB_CONNECTION_STRING"(e.g., "mongodb://root:root@localhost:27017")"MONGODB_DATABASE_NAME"(e.g., "my_mongodb")
Interfaces
Services
namespace Mongo.GenericClient.Core.Services
{
using System.Collections.Generic;
using System.Threading.Tasks;
using Mongo.GenericClient.Core.Entities;
using Mongo.GenericClient.Models;
using MongoDB.Bson;
public interface IReadService<TEntity>
where TEntity : IEntity
{
IEnumerable<TEntity> GetAll(Expression<Func<TEntity, bool>>? filter = null);
Task<PaginationResult<TEntity>> GetPaginatedAsync(
int pageNum,
int pageSize);
Task<PaginationResult<TEntity>> GetPaginatedAsync(
Expression<Func<TEntity, bool>> filter,
int pageNum,
int pageSize);
Task<TEntity> GetByIdAsync(ObjectId id);
Task<TEntity> GetByIdAsync(string id);
IMongoQueryable<TEntity> AsQueryable();
long CountDocuments(Expression<Func<TEntity, bool>>? filter = null);
}
}
namespace Mongo.GenericClient.Core.Services
{
using System.Threading.Tasks;
using Mongo.GenericClient.Core.Entities;
using MongoDB.Bson;
public interface IWriteService<TEntity>
where TEntity : IEntity
{
Task<TEntity> CreateAsync(TEntity entity);
Task<bool> UpdateAsync(TEntity entity);
Task DeleteAsync(ObjectId id);
Task DeleteAsync(string id);
}
}
IEntity
namespace Mongo.GenericClient.Core.Entities
{
using MongoDB.Bson;
public interface IEntity
{
ObjectId Id { get; set; }
}
}
IMongoContext
namespace Mongo.GenericClient.Core
{
using Mongo.GenericClient.Core.Entities;
using MongoDB.Driver;
public interface IMongoContext
{
IMongoCollection<TEntity> GetCollection<TEntity>()
where TEntity : IEntity;
}
}
MongoHelper
Note: Auxiliary helper that provides access to the MongoClient and the Database directly, if needed.
namespace Mongo.GenericClient
{
using MongoDB.Driver;
public static class MongoHelper
{
public static MongoClient Client => new MongoClient(AppConfig.ConnectionString);
public static IMongoDatabase Database => Client.GetDatabase(AppConfig.DatabaseName);
}
}
Usage
First, register IMongoContext and the services for all the entities to be used.
using Mongo.GenericClient.DependencyInjection;
// IMongoContext
services.RegisterMongoContext(RegisterMode.Singleton);
// Services (specifying the entities)
services.RegisterGenericServices<ExampleEntity>(RegisterMode.Singleton);
services.RegisterGenericServices<AnotherEntity>(RegisterMode.Scoped);
// Services (for all entities at the same time)
services.RegisterAllGenericServices(RegisterMode.Scoped);
Defining an entity and its collection name
The class must implement
IEntityand have the attributeCollectionName.
namespace Mongo.GenericClient.Tests.Setup
{
using Mongo.GenericClient.Core.Attributes;
using Mongo.GenericClient.Core.Entities;
using MongoDB.Bson;
[CollectionName("persons")]
public class PersonEntity : IEntity
{
public ObjectId Id { get ; set; }
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
}
}
Creating a collection (or just inserting data)
Using IMongoContext
await this.mongoContext.GetCollection<PersonEntity>().InsertOneAsync(entity);
Using the service
await this.writeService.CreateAsync(entity);
Getting a list of documents as entities
var allEntities = this.readService.GetAll();
var filteredEntities = this.readService.GetAll(x => x.Age == 30);
Note
In order to run the tests, you must first run
docker compose up -d.You can then manage the database at http://localhost:8081, with:
- User: user
- Password: pwd
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- MongoDB.Driver (>= 2.29.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 |
|---|---|---|
| 10.1.5 | 82 | 9/4/2026 |
| 10.1.4 | 105 | 7/31/2026 |
| 10.1.3 | 103 | 5/17/2026 |
| 10.1.2 | 122 | 4/6/2026 |
| 10.1.1 | 115 | 2/28/2026 |
| 10.1.0 | 122 | 1/11/2026 |
| 10.0.1 | 120 | 1/3/2026 |
| 10.0.0 | 147 | 12/6/2025 |
| 9.0.4 | 198 | 9/20/2025 |
| 9.0.3 | 134 | 5/10/2025 |
| 9.0.2 | 167 | 3/28/2025 |
| 9.0.1 | 155 | 2/23/2025 |
| 9.0.0 | 163 | 12/8/2024 |
| 8.3.0 | 154 | 11/22/2024 |
| 8.2.0 | 154 | 11/1/2024 |
| 8.1.8 | 145 | 10/12/2024 |
| 8.1.7 | 158 | 10/9/2024 |
| 8.1.6 | 163 | 9/29/2024 |
| 8.1.5 | 159 | 9/29/2024 |
| 8.1.4 | 157 | 9/28/2024 |