MongoMultiConnector 1.0.0

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

MongoMultiConnector

MongoMultiConnector is a C# library designed to facilitate the connection and interaction with multiple MongoDB databases. The project targets .NET 8.0 and leverages the MongoDB.Driver library for MongoDB operations.

Key Components

1. Extensions

  • ServiceCollectionExtension: Provides extension methods for IServiceCollection to configure and register MongoDB services.

2. Interfaces

  • IMongoDbRepository: Defines the contract for accessing MongoDB hosts and databases.

3. Providers

  • MongoDbRepository: Implements the IMongoDbRepository interface and provides methods to interact with MongoDB databases.

Configuration

1. Registering the Service

Add the following lines to your service configuration (e.g., in Program.cs): To register the MongoMultiConnector tool with and without collections, you can use the AddMongoMultiConnectorTool method.

builder.Services.AddMongoMultiConnectorTool(configuration);

2. MongoDbMultiConnectorConfig

The configuration for MongoDB connections is defined in the MongoDbMultiConnectorConfig class. This includes host details, database names. Below is a detailed explanation of its properties:

Properties

HostConfigs

  • Type: List<HostConfig>
  • Description: A list of host configurations, each defining the connection details for a MongoDB host and its associated databases.
HostConfig Properties
Host
  • Type: string
  • Description: The hostname or IP address of the MongoDB server.
Port
  • Type: int
  • Description: The port number on which the MongoDB server is listening.
DataBase
  • Type: string
  • Description: The name of the database to connect to on the MongoDB server.

Example Configuration

In appsettings.json

{
  "MongoDbMultiConnectorConfig": {
    "HostConfigs": [
      {
        "Host": "mongodb://localhost",
        "DataBase": "AuthDb",
        "Port": 27017,
        "Alias": "Auth"
      },
      {
        "Host": "mongodb://localhost",
        "DataBase": "MainDb",
        "Port": 27017,
        "Alias": "Main"
      }
    ]
  }
}

MongoMultiConnector Usage

This documentation provides examples of how to use the MongoMultiConnector library to access MongoDB databases and or their associated models.

Accessing Databases

Retrieving a Database

To retrieve a MongoDB database, use the GetDb extension method. This method requires the database alias name.

Example Usage in Controller

[ApiController]
[Route("api/[controller]")]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ApiResponse<IEmptyDto>))]


public class MongConnectionsController(IMongoDbRepository mongoDbRepository) : ControllerBase
{
   
    [HttpGet]
    [Route("do-mongo-thing")]
    [Produces(MediaTypeNames.Application.Json)]
    [Consumes(MediaTypeNames.Application.Json)]
    [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ApiResponse<IEmptyDto>))]
    [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ApiResponse<IEmptyDto>))]
    
    public async Task<IActionResult> TestMongoDb()
    {
       var authDbUsersCollection =  mongoDbRepository.GetDb("Auth");
       var mainDbBooksCollection =  mongoDbRepository.GetDb("Main");
       const string  mainModel = "books-collection";
       const string userModel = "users-collection";
        
        // Create
        var newUser = new User { Name = "John Doe" };
        var newBook = new Book { Title = "The Great Book" };
        
        await authDbUsersCollection.GetCollection<User>(userModel).InsertOneAsync(newUser);
        await mainDbBooksCollection.GetCollection<Book>(mainModel).InsertOneAsync(newBook);
       
        // Read
        var user = await authDbUsersCollection.GetCollection<User>(userModel).Find(u => u.Name == "John Doe").FirstOrDefaultAsync();
        var book = await mainDbBooksCollection.GetCollection<Book>(mainModel).Find(b => b.Title == "The Great Book").FirstOrDefaultAsync();
        
        // Update
        var update = Builders<User>.Update.Set(u => u.Name, "Jane Doe");
        await authDbUsersCollection.GetCollection<User>(userModel).UpdateOneAsync(u => u.Name == "John Doe", update);
        
        var bookUpdate = Builders<Book>.Update.Set(b => b.Title, "The Greatest Book");
        await mainDbBooksCollection.GetCollection<Book>(mainModel).UpdateOneAsync(b => b.Title == "The Great Book", bookUpdate);
        
        // Delete
        await authDbUsersCollection.GetCollection<User>(userModel).DeleteOneAsync(u => u.Name == "Jane Doe");
        await mainDbBooksCollection.GetCollection<Book>(mainModel).DeleteOneAsync(b => b.Title == "The Greatest Book");
     
        return Ok(new {user,book});
    }
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 194 2/23/2025

#connect to multiple mongo databases