SwitchSoftware.Repositories.MongoDb 8.0.3

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

SwitchSoftware.Repositories.MongoDb

SwitchSoftware.Repositories.MongoDb provides a MongoDB implementation of the SwitchSoftware.Repositories repository pattern.

It helps you work with MongoDB collections through a consistent repository abstraction, with support for CRUD operations, filtering, pagination, transactions, counting, and index creation.

Features

  • MongoDB repository implementation for entities derived from EntityBase<TId>
  • Create, retrieve, update, and delete operations
  • Query support using expressions or MongoDB filter definitions
  • Paginated queries with sorting
  • Document counting
  • Transaction execution helpers
  • Collection index creation support

Installation

Package Manager:

Install-Package SwitchSoftware.Repositories.MongoDb

.NET CLI:

dotnet add package SwitchSoftware.Repositories.MongoDb

Requirements

  • .NET Standard 2.1 compatible application
  • A MongoDB instance accessible through a connection string
  • MongoDB.Driver 3.x (transitive dependency � included automatically)
  • The base abstractions from SwitchSoftware.Repositories

Quick Start

using System;
using Microsoft.Extensions.Logging.Abstractions;
using SwitchSoftware.Repositories.MongoDb;

public class Product : EntityBase<Guid>
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

var repository = new MongoDbRepository<Product, Guid>(
    connectionString: "mongodb://localhost:27017",
    databaseName: "catalog",
    collectionName: "products",
    logger: NullLogger<MongoDbRepository<Product, Guid>>.Instance);
// guidRepresentation defaults to GuidRepresentation.Standard (UUID)

var id = Guid.NewGuid();

await repository.Create(new Product
{
    Id = id,
    Name = "Mechanical Keyboard",
    Price = 129.99m
}, id);

var product = await repository.Retrieve(id);

Common Operations

Create

var id = Guid.NewGuid();

await repository.Create(new Product
{
    Id = id,
    Name = "Wireless Mouse",
    Price = 49.99m
}, id);

Retrieve by Id

var product = await repository.Retrieve(id);

Update

product.Price = 44.99m;
await repository.Update(product);

Delete

await repository.Delete(id);

Get All

var products = await repository.Get();

Filtered Queries

Using a LINQ expression:

var activeProducts = await repository.Get(p => p.Price > 0);

Using a MongoDB filter definition:

using MongoDB.Driver;

var filter = Builders<Product>.Filter.Gt(p => p.Price, 100);
var premiumProducts = await repository.Get(filter);

Pagination

The orderByFields parameter uses the format Field:a|OtherField:d, where:

  • a = ascending
  • d = descending
using MongoDB.Driver;

var filter = Builders<Product>.Filter.Empty;
var page = await repository.GetPaginated(filter, "Name:a", 20, 0);

Count Documents

using MongoDB.Driver;

var count = await repository.CountItems(Builders<Product>.Filter.Gt(p => p.Price, 100));

Transactions

You can execute repository operations inside a MongoDB transaction using WithTransaction.

var result = await repository.WithTransaction(() =>
{
    var id = Guid.NewGuid();

    repository.Create(new Product
    {
        Id = id,
        Name = "Transactional Item",
        Price = 10m
    }, id).GetAwaiter().GetResult();

    var saved = repository.Retrieve(id).GetAwaiter().GetResult();
    return saved.Name;
});

Index Creation

Use AddIndexes to create one or more indexes on the collection.

using MongoDB.Driver;

var indexes = new[]
{
    new CreateIndexModel<Product>(
        Builders<Product>.IndexKeys.Ascending(p => p.Name))
};

repository.AddIndexes(indexes);

Guid Serialization Important Note

By default the repository registers GuidRepresentation.Standard (UUID format), which is the recommended format for new applications using MongoDB.Driver 3.x.

If you are working with an existing database that stored Guid values using MongoDB.Driver 2.x (which defaulted to CSharpLegacy), pass the matching representation to the constructor to maintain read/write compatibility:

using MongoDB.Bson;
using Microsoft.Extensions.Logging.Abstractions;
using SwitchSoftware.Repositories.MongoDb;

var repository = new MongoDbRepository<Product, Guid>(
    connectionString: "mongodb://localhost:27017",
    databaseName: "catalog",
    collectionName: "products",
    logger: NullLogger<MongoDbRepository<Product, Guid>>.Instance,
    guidRepresentation: GuidRepresentation.CSharpLegacy);

Available values are any member of MongoDB.Bson.GuidRepresentation: Standard, CSharpLegacy, JavaLegacy, PythonLegacy, and Unspecified.

When to Use This Package

This package is a good fit when you want to:

  • Keep a repository abstraction in applications that use MongoDB
  • Reuse patterns from SwitchSoftware.Repositories
  • Centralize MongoDB collection access behind strongly typed repositories
  • Add pagination, filtering, and index management without repeating infrastructure code

Package Summary

SwitchSoftware.Repositories.MongoDb is a lightweight repository implementation for MongoDB focused on practical application use cases and consistent data-access patterns.

Upgrade Notes

8.0.3

  • Upgraded MongoDB.Driver dependency from 2.28.0 to 3.8.1.
  • GuidRepresentation breaking change: MongoDB.Driver 3.x removed the global GuidRepresentation setting. This library now defaults to GuidRepresentation.Standard (UUID). If your application has existing data stored with a MongoDB.Driver 2.x installation, Guid fields were written using CSharpLegacy format by default and will not be read correctly without migration or configuration. To maintain compatibility, pass guidRepresentation: GuidRepresentation.CSharpLegacy to the constructor. See the Guid Serialization section for details.
  • Since IMongoDbRepository exposes FilterDefinition<TEntity> and CreateIndexModel<TEntity>, consuming projects must now reference MongoDB.Driver 3.x.

8.0.2

  • Added AddIndexes method.

8.0.1

  • Upgraded MongoDB.Driver to 2.28.0.
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 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. 
.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

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
8.0.3 103 5/26/2026
8.0.3-beta 203 5/20/2026
8.0.3-alpha 106 5/19/2026
8.0.2 1,371 5/18/2026
8.0.2-rc 41,711 4/8/2025
8.0.2-beta2 220 4/8/2025
8.0.2-beta 234 4/8/2025
8.0.1 20,583 7/24/2024
8.0.0 369 7/15/2024
7.0.5 5,383 6/15/2023
7.0.4 676 5/15/2023
7.0.3 319 5/12/2023
7.0.2 315 5/12/2023
7.0.1 555 11/28/2022
7.0.0 511 11/9/2022
6.0.5 6,837 6/15/2023
6.0.1 3,211 11/28/2022
6.0.0 5,875 5/23/2022
5.0.0 627 5/23/2022
3.1.25 686 5/23/2022
Loading failed