MandalaConsulting.Repository.Mongo 0.0.6

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

MongoHelper

Overview

The MongoHelper library, developed by Mandala Consulting, LLC, provides a simple and efficient wrapper for MongoDB database operations in .NET. It offers utility methods to handle common MongoDB operations such as creating, reading, updating, and deleting documents, alongside helper functions for managing connections and logging.


Features

  • Simplifies MongoDB connection setup.
  • CRUD operations for MongoDB documents:
    • Create: Insert documents.
    • Read: Fetch documents using filters or retrieve all documents.
    • Update: Modify existing documents.
    • Delete: Remove documents based on filters.
  • Logging functionality to capture MongoDB activity.
  • Event-driven log management (LogAdded, LogCleared).
  • Connection string builder for secure MongoDB access.
  • Helper methods to extract document IDs dynamically.

Installation

Clone the repository and include the library in your project. Ensure you have the following dependencies installed:

  • MongoDB.Driver
  • MandalaConsulting.Optimization.Logging

Usage

Setting Up MongoHelper

using MandalaConsulting.Repository.Mongo;

// Initialize with database name and connection string
MongoHelper mongoHelper = new MongoHelper("YourDatabaseName", "YourConnectionString");

Connection String Builder

You can dynamically create a secure connection string:

string connectionString = MongoHelper.ConnectionStringBuilder(
    "username",
    "password",
    "clusterName",
    "region"
);

Basic CRUD Operations

Create a Document
await mongoHelper.CreateDocumentAsync("CollectionName", new { Name = "John Doe", Age = 30 });
Read Documents

Fetch all documents:

var documents = await mongoHelper.GetAllDocumentsAsync<MyDocument>("CollectionName");

Fetch documents with a filter:

var filter = Builders<MyDocument>.Filter.Eq(doc => doc.Name, "John Doe");
var filteredDocuments = await mongoHelper.GetFilteredDocumentsAsync("CollectionName", filter);
Update a Document
var filter = Builders<MyDocument>.Filter.Eq(doc => doc.Id, 1);
var update = Builders<MyDocument>.Update.Set(doc => doc.Name, "Jane Doe");
await mongoHelper.UpdateDocumentAsync("CollectionName", filter, update);
Delete a Document
var filter = Builders<MyDocument>.Filter.Eq(doc => doc.Id, 1);
await mongoHelper.DeleteDocumentAsync("CollectionName", filter);

Testing Database Connection

List<string> collections = mongoHelper.TestConnection();
if (collections != null)
{
    Console.WriteLine("Connection successful!");
}

Events

LogAdded Event

Triggered when a new log is added:

MongoHelper.LogAdded += (sender, args) =>
{
    Console.WriteLine($"New Log: {args.LogMessage}");
};

LogCleared Event

Triggered when logs are cleared:

MongoHelper.LogCleared += (sender, args) =>
{
    Console.WriteLine("Logs cleared.");
};

Logging

Retrieve Logs

var logs = MongoHelper.GetLogs();
foreach (var log in logs)
{
    Console.WriteLine(log.Message);
}

Clear Logs

MongoHelper.ClearLogs();

Requirements

  • .NET Framework or .NET Core
  • MongoDB Instance
  • NuGet Packages:
    • MongoDB.Driver
    • MandalaConsulting.Optimization.Logging

License

This project is licensed under the MIT License. See the LICENSE file for details.


Author

Created by Alexander Fields
Copyright © 2023 Mandala Consulting, LLC

For inquiries, please contact Mandala Consulting.

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
0.0.6 288 7/5/2025
0.0.5 353 6/8/2025
0.0.4 126 5/9/2025
0.0.3 648 10/15/2024
0.0.2 110 10/15/2024
0.0.1 164 10/12/2024