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
<PackageReference Include="AnointedAutomation.Repository.Mongo" Version="0.0.10" />
<PackageVersion Include="AnointedAutomation.Repository.Mongo" Version="0.0.10" />
<PackageReference Include="AnointedAutomation.Repository.Mongo" />
paket add AnointedAutomation.Repository.Mongo --version 0.0.10
#r "nuget: AnointedAutomation.Repository.Mongo, 0.0.10"
#:package AnointedAutomation.Repository.Mongo@0.0.10
#addin nuget:?package=AnointedAutomation.Repository.Mongo&version=0.0.10
#tool nuget:?package=AnointedAutomation.Repository.Mongo&version=0.0.10
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.DriverAnointedAutomation.ObjectsAnointedAutomation.LoggingNewtonsoft.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
UserIdas 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 | 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
- AnointedAutomation.Logging (>= 0.0.8)
- AnointedAutomation.Objects (>= 0.0.21)
- MongoDB.Bson (>= 3.6.0)
- MongoDB.Driver (>= 3.6.0)
- MongoDB.Driver.Core (>= 2.30.0)
- MongoDB.Driver.GridFS (>= 2.30.0)
- MongoDB.Libmongocrypt (>= 1.12.0)
- Newtonsoft.Json (>= 13.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.