Vestfold.Extensions.MongoDb 1.0.0

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

NuGet Version NuGet Downloads

Vestfold.Extensions.MongoDb

Contains builder extensions to extend a dotnet core application with MongoDB functionality.

Setting up for an Azure Function / Azure Web App

Add the following to your local.settings.json file:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "MONGODB_CONNECTION_STRING": "mongodb+srv://<db_username>:<db_password>@<db_server>/?retryWrites=true&w=majority&appName=<app_name>",
    "MONGODB_CACHE_EXPIRATION_MINUTES": "<optional cache expiration in minutes, default is 60>"
  }
}

Add the following to your Program.cs file:

var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.Services.AddVestfoldMongoDb();
builder.Build().Run();

Setting up for a HostBuilder (Console app, ClassLibrary, etc.)

Add the following to your appsettings.json file:

{
  "MONGODB_CONNECTION_STRING": "mongodb+srv://<db_username>:<db_password>@<db_server>/?retryWrites=true&w=majority&appName=<app_name>",
  "MONGODB_CACHE_EXPIRATION_MINUTES": "<optional cache expiration in minutes, default is 60>"
}

Add the following to your Program.cs file:

public static async Task Main(string[] args)
{
    await Host.CreateDefaultBuilder(args)
        .ConfigureServices(services => services.AddVestfoldMongoDb())
        .Build()
        .RunAsync();
}

Setting up for a WebApplicationBuilder (WebAPI, Blazor, etc.)

Add the following to your appsettings.json file:

{
  "MONGODB_CONNECTION_STRING": "mongodb+srv://<db_username>:<db_password>@<db_server>/?retryWrites=true&w=majority&appName=<app_name>",
  "MONGODB_CACHE_EXPIRATION_MINUTES": "<optional cache expiration in minutes, default is 60>"
}

Add the following to your Program.cs file:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddVestfoldMongoDb();

var app = builder.Build();

Retrieve a MongoDB collection to work with in your own code base

Inject the IMongoDbService into your classes.

Then you call GetMongoCollection<T>("databaseName", "collectionName") to retrieve a collection of type T.

There is also parameters for passing along a MongoDatabaseSettings object and a MongoCollectionSettings object if you need to customize the database or collection settings. But these are optional.

public record MyEntity(string Id, string Name);

public class Something
{
    private readonly IMongoDbService _mongoDbService;
    
    public Something(IMongoDbService mongoDbService)
    {
        _mongoDbService = mongoDbService;
    }
    
    public async Task DoSomething()
    {
        // retrieve a collection of type MyEntity from the database "databaseName" and collection "collectionName"
        var collection = await _mongoDbService.GetMongoCollection<MyEntity>("databaseName", "collectionName");
        
        // specify a filter
        var filter = Builders<MyEntity>.Filter.Empty;
        
        // find documents in the collection that match the filter
        var documents = (await collection.FindAsync(filter)).ToList();
        
        // do something with the documents
        foreach (var document in documents)
        {
            Console.WriteLine($"Id: {document.Id}, Name: {document.Name}");
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET 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. 
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
1.0.0 102 7/4/2025