FGV.Lib.Persistence.MongoDb
1.0.0
dotnet add package FGV.Lib.Persistence.MongoDb --version 1.0.0
NuGet\Install-Package FGV.Lib.Persistence.MongoDb -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FGV.Lib.Persistence.MongoDb" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FGV.Lib.Persistence.MongoDb" Version="1.0.0" />
<PackageReference Include="FGV.Lib.Persistence.MongoDb" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FGV.Lib.Persistence.MongoDb --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: FGV.Lib.Persistence.MongoDb, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FGV.Lib.Persistence.MongoDb@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FGV.Lib.Persistence.MongoDb&version=1.0.0
#tool nuget:?package=FGV.Lib.Persistence.MongoDb&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
FGV.Lib.Persistence.MongoDb
A lightweight, flexible MongoDB persistence library that provides a robust repository pattern implementation for .NET applications. This library simplifies MongoDB database operations while promoting clean architecture and separation of concerns.
Features
- Generic repository pattern implementation for MongoDB
- Async/await support for all operations
- CRUD operations (Create, Read, Update, Delete)
- Filtering by expression
- Pagination support
- Strongly-typed document mapping
- Flexible configuration options
Installation
Via NuGet Package Manager
Install-Package FGV.Lib.Persistence.MongoDb
Via .NET CLI
dotnet add package FGV.Lib.Persistence.MongoDb
Via PackageReference in .csproj
<PackageReference Include="FGV.Lib.Persistence.MongoDb" Version="x.x.x" />
Configuration
Adding MongoDB to your services (Dependency Injection)
In your Startup.cs
or Program.cs
file:
using FGV.Lib.Persistence.MongoDb;
using FGV.Lib.Persistence.MongoDb.Interface;
// ...
public void ConfigureServices(IServiceCollection services)
{
// Configure MongoDB
services.AddSingleton<IMongoClient>(sp => new MongoClient(Configuration.GetConnectionString("MongoDb")));
// Register the repository
services.AddScoped<IMongoRepository<YourEntity>, MongoRepository<YourEntity>>();
// Alternative: register for multiple entity types
// services.AddScoped(typeof(IMongoRepository<>), typeof(MongoRepository<>));
}
Configuration in appsettings.json
{
"ConnectionStrings": {
"MongoDb": "mongodb://username:password@localhost:27017/yourdatabase"
},
"MongoDbSettings": {
"DatabaseName": "YourDatabaseName",
"CollectionNamingConvention": "PascalCase" // Optional
}
}
Usage
Define your entity
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
public class Product
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public bool IsAvailable { get; set; }
}
Basic CRUD Operations
// Inject the repository in your constructor
private readonly IMongoRepository<Product> _productRepository;
public ProductService(IMongoRepository<Product> productRepository)
{
_productRepository = productRepository;
}
// Create
public async Task AddProductAsync(Product product)
{
await _productRepository.InsertAsync(product);
}
// Read (Get by ID)
public async Task<Product> GetProductByIdAsync(string id)
{
return await _productRepository.GetByIdAsync(id);
}
// Read (Get all)
public async Task<IEnumerable<Product>> GetAllProductsAsync()
{
return await _productRepository.GetAllAsync();
}
// Update
public async Task UpdateProductAsync(Product product)
{
await _productRepository.UpdateAsync(product);
}
// Delete
public async Task DeleteProductAsync(string id)
{
await _productRepository.DeleteAsync(id);
}
Filtering and Querying
// Get products by category
public async Task<IEnumerable<Product>> GetProductsByCategoryAsync(string category)
{
return await _productRepository.FilterByAsync(p => p.Category == category);
}
// Get available products with price less than specified amount
public async Task<IEnumerable<Product>> GetAvailableProductsUnderPriceAsync(decimal maxPrice)
{
return await _productRepository.FilterByAsync(p => p.Price < maxPrice && p.IsAvailable);
}
// Get single product that matches criteria
public async Task<Product> GetUniqueProductAsync(string name)
{
return await _productRepository.FindOneAsync(p => p.Name == name);
}
Advanced Usage
Custom Collection Name
[BsonCollection("custom_products")]
public class Product
{
// Properties...
}
Pagination
// Get products with pagination
public async Task<IEnumerable<Product>> GetProductsPagedAsync(int pageNumber, int pageSize)
{
return await _productRepository.GetAllAsync(pageNumber, pageSize);
}
Working with Count
// Count all products
public async Task<long> CountAllProductsAsync()
{
return await _productRepository.CountAsync();
}
// Count products that match a condition
public async Task<long> CountAvailableProductsAsync()
{
return await _productRepository.CountAsync(p => p.IsAvailable);
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- FGV.Lib.Helpers (>= 1.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- MongoDB.Driver (>= 2.24.0)
- Newtonsoft.Json (>= 13.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.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 152 | 6/2/2025 |