MongoDB.Abstracts 6.5.0

dotnet add package MongoDB.Abstracts --version 6.5.0
                    
NuGet\Install-Package MongoDB.Abstracts -Version 6.5.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.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MongoDB.Abstracts" Version="6.5.0" />
                    
Directory.Packages.props
<PackageReference Include="MongoDB.Abstracts" />
                    
Project file
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 MongoDB.Abstracts --version 6.5.0
                    
#r "nuget: MongoDB.Abstracts, 6.5.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.
#addin nuget:?package=MongoDB.Abstracts&version=6.5.0
                    
Install MongoDB.Abstracts as a Cake Addin
#tool nuget:?package=MongoDB.Abstracts&version=6.5.0
                    
Install MongoDB.Abstracts as a Cake Tool

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.  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. 
.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 (2)

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

Package Downloads
MediatR.CommandQuery.MongoDB

CQRS framework based on MediatR

Arbiter.CommandQuery.MongoDB

Command Query Responsibility Segregation (CQRS) framework based on mediator pattern using Mongo DB as the data store handler

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.5.0 188 5/25/2025
6.4.0 291 4/18/2025
6.3.0 407 2/4/2025
6.2.0 110 2/4/2025
6.1.0 115 2/3/2025
6.0.0 111 2/3/2025
5.7.1 698 7/26/2024
5.7.0 469 5/10/2024
5.6.1 725 7/28/2023
5.5.269 1,033 12/28/2022
5.5.219 957 4/23/2022
5.5.216 687 3/21/2022
5.5.191 800 12/22/2021
5.5.172 1,192 10/29/2021
5.0.0.166 403 10/21/2021
5.0.0.128 391 6/5/2021
4.0.0.102 483 2/15/2021
4.0.0.83 540 10/26/2020
4.0.0.80 555 10/17/2020
3.0.0.79 567 10/17/2020
3.0.0.56 710 5/9/2020
3.0.0.36 828 3/16/2020
2.0.0.10 837 4/23/2019
2.0.0.8 1,129 4/27/2017
2.0.0.7 1,098 4/4/2017
1.0.0.5 1,190 5/4/2016
1.0.0.3 1,151 4/30/2016
1.0.0.2 1,372 4/21/2016