UEntity.MongoDb
3.1.0
See the version list below for details.
dotnet add package UEntity.MongoDb --version 3.1.0
NuGet\Install-Package UEntity.MongoDb -Version 3.1.0
<PackageReference Include="UEntity.MongoDb" Version="3.1.0" />
paket add UEntity.MongoDb --version 3.1.0
#r "nuget: UEntity.MongoDb, 3.1.0"
// Install UEntity.MongoDb as a Cake Addin #addin nuget:?package=UEntity.MongoDb&version=3.1.0 // Install UEntity.MongoDb as a Cake Tool #tool nuget:?package=UEntity.MongoDb&version=3.1.0
UEntity.MongoDb NuGet Package
Introduction
The UEntity.MongoDb
NuGet package provides a flexible and efficient repository pattern implementation for MongoDB. This package simplifies common MongoDB operations such as querying, inserting, updating, deleting, and pagination.
Getting Started
Installation
Install the package via NuGet:
Install-Package UEntity.MongoDb
Configuration
To use the package, configure it in your Program.cs
file by calling the AddUEntityMongoDb
extension method.
using MongoDB.Driver;
using Microsoft.Extensions.DependencyInjection;
using UEntity.MongoDb;
var services = new ServiceCollection();
var mongoClient = new MongoClient("YourMongoDbConnectionString");
services.AddUEntityMongoDb(mongoClient);
Usage
Repository Methods
The IEntityRepositoryMongo<T>
interface provides the following methods for MongoDB operations:
1. Get
Retrieves a single document based on a filter.
var repository = new EntityRepositoryMongo<MyEntity>("DatabaseName");
var result = repository.Get(x => x.Id == "12345");
2. GetAsync
Asynchronously retrieves a single document based on a filter.
var result = await repository.GetAsync(x => x.Id == "12345");
3. GetAll
Retrieves all documents matching a filter.
var results = repository.GetAll(x => x.IsActive);
4. GetAllAsync
Asynchronously retrieves all documents matching a filter.
var results = await repository.GetAllAsync(x => x.IsActive);
5. GetListPaginate
Retrieves paginated results.
var paginatedResults = repository.GetListPaginate(0, 10, filter: null, sort: null);
6. Add
Inserts a single document into the collection.
var entity = new MyEntity { Id = "12345", Name = "Test" };
repository.Add(entity);
7. AddAsync
Asynchronously inserts a single document into the collection.
await repository.AddAsync(entity);
8. Update
Updates a document based on a filter.
var updateResult = repository.Update(x => x.Id == "12345", updatedEntity);
9. Delete
Deletes a document based on a filter.
var deleteResult = repository.Delete(x => x.Id == "12345");
Advanced Topics
Sorting
Use the EntitySortModel<T>
to define sorting criteria:
var sortModel = new EntitySortModel<MyEntity>
{
Sort = x => x.Name,
IsDescending = false
};
var results = repository.GetAll(x => x.IsActive, sortModel);
Pagination
Efficiently retrieve paginated data with filters and sorting:
var paginatedData = await repository.GetListPaginateAsync(0, 10, filter: null, sort: sortModel);
Conclusion
The UEntity.MongoDb
package simplifies MongoDB operations while offering flexibility for advanced use cases. Integrate it into your projects to streamline database operations and enhance maintainability.
For additional support or contributions, refer to the repository's documentation or reach out to the maintainers.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
- MongoDB.Driver (>= 3.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.