AnointedAutomation.Repository.Mongo 0.0.10

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

MongoHelper

Overview

The MongoHelper library, developed by Anointed Automation, 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, logging, and BSON serialization.


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.
  • BSON Class Map Registration: Automatic configuration of base objects for MongoDB serialization.
  • JObject Serialization: Built-in support for Newtonsoft.Json JObject ↔ MongoDB BSON conversion.
  • 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

Install via NuGet:

dotnet add package AnointedAutomation.Repository.Mongo

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

  • MongoDB.Driver
  • AnointedAutomation.Objects
  • AnointedAutomation.Logging
  • Newtonsoft.Json

Usage

Setting Up BsonClassMapRegistrar (IMPORTANT)

Before performing any MongoDB operations, you must register the BSON class maps. Call this once at application startup:

using AnointedAutomation.Repository.Mongo;

// In your Program.cs or startup code:
BsonClassMapRegistrar.RegisterClassMaps();

This registers:

  • User class with UserId as the MongoDB document _id
  • JObjectSerializer for Newtonsoft.Json JObject properties
  • Proper inheritance handling for derived user classes

Registering Derived User Classes

If you have a custom user class that extends User, register it after the base class maps:

using AnointedAutomation.Repository.Mongo;

// Register base class maps first
BsonClassMapRegistrar.RegisterClassMaps();

// Then register your derived user class
BsonClassMapRegistrar.RegisterDerivedUser<MyCustomUser>();

This ensures proper BSON serialization for your custom user types in MongoDB.

Setting Up MongoHelper

using AnointedAutomation.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!");
}

Database-Agnostic Objects

This library is designed to work with pure POCO objects from AnointedAutomation.Objects. The BSON serialization is configured via BsonClassMap rather than attributes, allowing the same object classes to be used with:

  • MongoDB (using this library)
  • SQL Server (using Entity Framework Fluent API)
  • MySQL (using Entity Framework or Dapper)
  • Any other database (just add appropriate configuration)

No need for separate MongoUser, SqlUser, etc. classes - just use the base User class and configure serialization externally.


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 8.0 or later
  • MongoDB Instance
  • NuGet Packages:
    • MongoDB.Driver
    • AnointedAutomation.Objects
    • AnointedAutomation.Logging
    • Newtonsoft.Json

License

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


Author

Created by Alexander Fields Copyright © 2023 Anointed Automation, LLC

For inquiries, please contact Anointed Automation.

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.10 41 2/22/2026
0.0.9 36 2/21/2026
0.0.8 158 2/17/2026