Maniceraf.SimpleCoreMongoDbHelper 1.0.1

dotnet add package Maniceraf.SimpleCoreMongoDbHelper --version 1.0.1                
NuGet\Install-Package Maniceraf.SimpleCoreMongoDbHelper -Version 1.0.1                
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="Maniceraf.SimpleCoreMongoDbHelper" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Maniceraf.SimpleCoreMongoDbHelper --version 1.0.1                
#r "nuget: Maniceraf.SimpleCoreMongoDbHelper, 1.0.1"                
#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.
// Install Maniceraf.SimpleCoreMongoDbHelper as a Cake Addin
#addin nuget:?package=Maniceraf.SimpleCoreMongoDbHelper&version=1.0.1

// Install Maniceraf.SimpleCoreMongoDbHelper as a Cake Tool
#tool nuget:?package=Maniceraf.SimpleCoreMongoDbHelper&version=1.0.1                

Maniceraf.SimpleCoreMongoDbHelper

Maniceraf.SimpleCoreMongoDbHelper is a .NET Core-based class library created to simplify MongoDB operations within your application framework. This library provides fundamental implementations for common MongoDB database tasks.

Installation

To install Maniceraf.SimpleCoreMongoDbHelper, you can use NuGet Package Manager:

NuGet\Install-Package Maniceraf.SimpleCoreMongoDbHelper -Version 1.0.1

Usage

Initialization

To start using the Maniceraf.SimpleMongoDbHelper in your project, initialize an instance of the FastMongoDbHelperclass:

// Initialize the FastMongoDbHelper
var mongoDbHelper = new FastMongoDbHelper("connectionString", "databaseName");

// Get number of database in the Client.
var count = mongoDbHelper.DatabaseCount;

// Get list of database in the Client.
var list = mongoDbHelper.Databases;

var isConnected = mongoDbHelper.IsConnected;

// Create a WeatherForecast model class
public class WeatherForecast
{
    [BsonId]
    public ObjectId Id { get; set; }
    public DateOnly Date { get; set; }
    public int TemperatureC { get; set; }
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    public string? Summary { get; set; }
}

Basic Operations

The library supports basic MongoDB operations such as:

1. Get all databases in the Client:
// Get all databases with synchronous
var all = mongoDbHelper.GetAllDatabases();

// Get all databases with asynchronous
var all = await mongoDbHelper.GetAllDatabasesAsync();
2. Drop database by name in the Client:
// Drop database by name with synchronous
mongoDbHelper.DropDatabase("databaseName");

// Drop database by name with asynchronous
await mongoDbHelper.DropDatabaseAsync("databaseName");
3. Get all collections in the database:
// Get all collections with synchronous
var collections = mongoDbHelper.GetAllCollections();

// Get all collections with asynchronous
var collections = await mongoDbHelper.GetAllCollectionsAsync();
4. Create new a collection by name in the database:
// Create new a collection by name with synchronous
mongoDbHelper.CreateCollection("collectionName");

// Create new a collection by name with asynchronous
await mongoDbHelper.CreateCollectionAsync("collectionName");
5. Drop a collection by name in the database:
// Drop a collection by name with synchronous
mongoDbHelper.DropCollection("collectionName");

// Drop a collection by name with asynchronous
await mongoDbHelper.DropCollectionAsync("collectionName");
6. Get a collection in the database:
// Get a collection
mongoDbHelper.GetCollection<WeatherForecast>();
7. Get all documents in collection <T>:
// Get all with synchronous
var items = mongoDbHelper.GetAll<WeatherForecast>();
var items = mongoDbHelper.GetCollection<WeatherForecast>().GetAll();

// Get all with asynchronous
var items = await mongoDbHelper.GetAllAsync<WeatherForecast>();
var items = await mongoDbHelper.GetCollection<WeatherForecast>().GetAllAsync();
8. Get a document by Id in collection <T>:
string id = "65699649600d064a2924c5d9";

// Get a document by Id with synchronous
var item = mongoDbHelper.GetById<WeatherForecast>(id);
var item = mongoDbHelper.GetCollection<WeatherForecast>().GetById(id);

// Get a document by Id with asynchronous
var item = await mongoDbHelper.GetByIdAsync<WeatherForecast>(id);
var item = await mongoDbHelper.GetCollection<WeatherForecast>().GetByIdAsync(id);
9. Insert a document into collection <T>:
// Create entity
var model1 = new WeatherForecast
{
    Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
    Summary = "Sunny",
    TemperatureC = 32,
};

// Insert a document with synchronous
mongoDbHelper.Insert<WeatherForecast>(model1);
mongoDbHelper.GetCollection<WeatherForecast>().Insert(model1);

// Create entity
var model2 = new WeatherForecast
{
    Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
    Summary = "Sunny",
    TemperatureC = 32,
};

// Insert a document with asynchronous
await mongoDbHelper.InsertAsync<WeatherForecast>(model2);
await mongoDbHelper.GetCollection<WeatherForecast>().InsertAsync(model2);
10. Insert multiple documents into collection <T>:
// Create a entity list
var model1 = new List<WeatherForecast>
{
    new WeatherForecast
    {
        Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
        Summary = "Sunny",
        TemperatureC = 32,
    },
    new WeatherForecast
    {
        Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
        Summary = "Sunny",
        TemperatureC = 32,
    }
};

// Insert multiple documents with synchronous
mongoDbHelper.InsertMany<WeatherForecast>(model1);
mongoDbHelper.GetCollection<WeatherForecast>().InsertMany(model1);

// Create a entity list
var model2 = new List<WeatherForecast>
{
    new WeatherForecast
    {
        Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
        Summary = "Sunny",
        TemperatureC = 32,
    },
    new WeatherForecast
    {
        Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
        Summary = "Sunny",
        TemperatureC = 32,
    }
};

// Insert multiple documents with asynchronous
await mongoDbHelper.InsertManyAsync<WeatherForecast>(model2);
await mongoDbHelper.GetCollection<WeatherForecast>().InsertManyAsync(model2);
11. Update a document in collection <T>:
string id = "656a24ca8b7893066464b698";

// Create entity
var model1 = new WeatherForecast
{
    Id = new ObjectId("656a24ca8b7893066464b698"),
    Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
    Summary = "Sunny",
    TemperatureC = 32,
};

// Update a document with synchronous
mongoDbHelper.Update<WeatherForecast>(id, model1);
mongoDbHelper.GetCollection<WeatherForecast>().Update(id, model1);

// Create entity
var model2 = new WeatherForecast
{
    Id = new ObjectId("656a24ca8b7893066464b698"),
    Date = DateOnly.FromDateTime(DateTime.UtcNow.Date),
    Summary = "Sunny",
    TemperatureC = 32,
};

// Update a document with asynchronous
await mongoDbHelper.UpdateAsync<WeatherForecast>(id, model2);
await mongoDbHelper.GetCollection<WeatherForecast>().UpdateAsync(id, model2);
12. Delete all documents in collection <T>:
// Delete all documents with synchronous
mongoDbHelper.DeleteAll<WeatherForecast>();
mongoDbHelper.GetCollection<WeatherForecast>().DeleteAll();

// Delete all documents with asynchronous
await mongoDbHelper.DeleteAllAsync<WeatherForecast>();
await mongoDbHelper.GetCollection<WeatherForecast>().DeleteAllAsync();
13. Delete a document by Id in collection <T>:
// Delete a document by Id with synchronous
mongoDbHelper.DeleteById<WeatherForecast>("656a24ca8b7893066464b698");
mongoDbHelper.GetCollection<WeatherForecast>().DeleteById("656a24ca8b7893066464b698");

// Delete a document by Id with asynchronous
await mongoDbHelper.DeleteByIdAsync<WeatherForecast>("656a24ca8b7893066464b698");
await mongoDbHelper.GetCollection<WeatherForecast>().DeleteByIdAsync("656a24ca8b7893066464b698");

Contributing

Contributions are welcome! Feel free to raise issues or submit pull requests.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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.1 237 12/1/2023
1.0.0 121 12/1/2023