MongoMultiConnector 1.0.0

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});
    }

No packages depend on MongoMultiConnector.

#connect to multiple mongo databases

Version Downloads Last updated
1.0.0 0 1/30/2026