MongoDB.Abstracts 6.3.0

dotnet add package MongoDB.Abstracts --version 6.3.0                
NuGet\Install-Package MongoDB.Abstracts -Version 6.3.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="MongoDB.Abstracts" Version="6.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoDB.Abstracts --version 6.3.0                
#r "nuget: MongoDB.Abstracts, 6.3.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.
// Install MongoDB.Abstracts as a Cake Addin
#addin nuget:?package=MongoDB.Abstracts&version=6.3.0

// Install MongoDB.Abstracts as a Cake Tool
#tool nuget:?package=MongoDB.Abstracts&version=6.3.0                

MongoDB.Abstracts

Overview

The MongoDB Abstracts library defines abstract base classes for repository pattern.

Build status

NuGet Version

Coverage Status

Download

The MongoDB.Abstracts library is available on nuget.org via package name MongoDB.Abstracts.

To install MongoDB.Abstracts, run the following command in the Package Manager Console

PM> Install-Package MongoDB.Abstracts

More information about NuGet package available at https://nuget.org/packages/MongoDB.Abstracts

Features

  • interface for generic MongoDB queries; IMongoQuery<TEntity, TKey>
  • interface for generic MongoDB repository; IMongoRepository<TEntity, TKey>
  • base class for generic MongoDB queries; MongoQuery<TEntity, TKey>
  • base class for generic MongoDB repository; MongoRepository<TEntity, TKey>
  • interface for generic MongoDB entity; IMongoEntity
  • base class for generic MongoDB entity; MongoEntity
  • interface for generic MongoDB entity repository; IMongoEntityRepository<TEntity>
  • base class for generic MongoDB entity repository; MongoEntityRepostiory<TEntity>

Configuration

Register with dependency injection

services.AddMongoRepository("mongodb://localhost:27017/UnitTesting");

Register using a connection name from the appsettings.json

services.AddMongoRepository("UnitTesting");
{
  "ConnectionStrings": {
    "UnitTesting": "mongodb://localhost:27017/UnitTesting"
  }
}
Discriminators

Register with discriminator type for support for multiple connections. This is a similar concept to dependency injection with keyed services.

Simple types used to discriminate connections

public readonly struct ProductsConnection;
public readonly struct InventoryConnection;

Register the MongoDB connections using the discriminators

services.AddMongoRepository<ProductsConnection>("ProductsConnection");
services.AddMongoRepository<InventoryConnection>("InventoryConnection");

Connection string in appsettings.json

{
  "ConnectionStrings": {
    "ProductsConnection": "mongodb://localhost:27017/Products",
    "InventoryConnection": "mongodb://localhost:27017/Inventory"
  }
}

Inject into constructor

public class ProductService
{
    private readonly IMongoEntityRepository<ProductsConnection, Product> _repository;

    public ProductService(IMongoEntityRepository<ProductsConnection, Product> repository)
    {
        _repository = repository;
    }
}

public class InventoryService
{
    private readonly IMongoEntityRepository<InventoryConnection, Inventory> _repository;

    public InventoryService(IMongoEntityRepository<InventoryConnection, Inventory> repository)
    {
        _repository = repository;
    }
}

Usage

Find an entity by key

// dependency inject
var roleRepo = Services.GetRequiredService<IMongoEntityRepository<Role>>();

// find by key
var role = await roleRepo.FindAsync("67a0dc52fa5ebe49f293a374");

Find one entity with query

// dependency inject
var roleRepo = Services.GetRequiredService<IMongoEntityRepository<Role>>();

// find one by query expression
var role = await roleRepo.FindOneAsync(r => r.Name.StartsWith("Admin"))

Find many with query

// dependency inject
var roleRepo = Services.GetRequiredService<IMongoEntityRepository<Role>>();

// find one by query expression
var roles = await roleRepo.FindAllAsync(r => r.Name.StartsWith("Admin"))

Use IQueryable

// dependency inject
var roleRepo = Services.GetRequiredService<IMongoEntityRepository<Role>>();

// Use IQueryable
var roles = roleRepo.All()
    .Where(r => r.IsActive)
    .ToList();

Insert entity

// dependency inject
var roleRepo = Services.GetRequiredService<IMongoEntityRepository<Role>>();

var role = new Role { Name = "CreateReadRole" };

var createdRole = await roleRepo.InsertAsync(role);

Update entity

// dependency inject
var roleRepo = Services.GetRequiredService<IMongoEntityRepository<Role>>();

// find by key
var role = await roleRepo.FindAsync("67a0dc52fa5ebe49f293a374");

// make changes
role.Name = "UpdateRole";

var updateRole = await roleRepo.UpdateAsync(role);

Delete entity by key

// dependency inject
var roleRepo = Services.GetRequiredService<IMongoEntityRepository<Role>>();

// items deleted
var count = await roleRepo.DeleteAsync("67a0dc52fa5ebe49f293a374");

Overrides

Create a custom repository

public class UserRepository : MongoEntityRepository<Models.User>
{
    public UserRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
    {
    }

    protected override void BeforeInsert(Models.User entity)
    {
        base.BeforeInsert(entity);

        entity.EmailLower = entity.Email?.ToLowerInvariant();
    }

    protected override void BeforeUpdate(Models.User entity)
    {
        base.BeforeUpdate(entity);

        entity.EmailLower = entity.Email?.ToLowerInvariant();
    }

    protected override void EnsureIndexes(IMongoCollection<Models.User> mongoCollection)
    {
        base.EnsureIndexes(mongoCollection);

        mongoCollection.Indexes.CreateOne(
            new CreateIndexModel<Models.User>(
                Builders<Models.User>.IndexKeys.Ascending(s => s.EmailLower),
                new CreateIndexOptions { Unique = true }
            )
        );
    }

}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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 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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MongoDB.Abstracts:

Package Downloads
MediatR.CommandQuery.MongoDB

CQRS framework based on MediatR

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.3.0 233 2/4/2025
6.2.0 90 2/4/2025
6.1.0 95 2/3/2025
6.0.0 91 2/3/2025
5.7.1 682 7/26/2024
5.7.0 453 5/10/2024
5.6.1 715 7/28/2023
5.5.269 1,018 12/28/2022
5.5.219 939 4/23/2022
5.5.216 668 3/21/2022
5.5.191 779 12/22/2021
5.5.172 1,169 10/29/2021
5.0.0.166 380 10/21/2021
5.0.0.128 365 6/5/2021
4.0.0.102 452 2/15/2021
4.0.0.83 509 10/26/2020
4.0.0.80 524 10/17/2020
3.0.0.79 535 10/17/2020
3.0.0.56 675 5/9/2020
3.0.0.36 793 3/16/2020
2.0.0.10 801 4/23/2019
2.0.0.8 1,058 4/27/2017
2.0.0.7 1,026 4/4/2017
1.0.0.5 1,118 5/4/2016
1.0.0.3 1,079 4/30/2016
1.0.0.2 1,299 4/21/2016