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
<PackageReference Include="Vestfold.Extensions.MongoDb" Version="1.0.0" />
<PackageVersion Include="Vestfold.Extensions.MongoDb" Version="1.0.0" />
<PackageReference Include="Vestfold.Extensions.MongoDb" />
paket add Vestfold.Extensions.MongoDb --version 1.0.0
#r "nuget: Vestfold.Extensions.MongoDb, 1.0.0"
#:package Vestfold.Extensions.MongoDb@1.0.0
#addin nuget:?package=Vestfold.Extensions.MongoDb&version=1.0.0
#tool nuget:?package=Vestfold.Extensions.MongoDb&version=1.0.0
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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.Caching.Memory (>= 9.0.6)
- Microsoft.Extensions.Configuration (>= 9.0.6)
- Microsoft.Extensions.DependencyInjection (>= 9.0.6)
- MongoDB.Driver (>= 3.4.0)
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 |