MongoCrud 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MongoCrud --version 1.2.0
NuGet\Install-Package MongoCrud -Version 1.2.0
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="MongoCrud" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoCrud --version 1.2.0
#r "nuget: MongoCrud, 1.2.0"
#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 MongoCrud as a Cake Addin
#addin nuget:?package=MongoCrud&version=1.2.0

// Install MongoCrud as a Cake Tool
#tool nuget:?package=MongoCrud&version=1.2.0

MongoCrud

MongoCrud ➕ 🔄️ ❌

MongoCrud is a simple c# class for MongoDB CRUD operations.

Nuget Nuget

Features

  • Create, read, update and delete documents
    • Create unique records
    • Case-Insensitive search
    • Read by index/Id
    • Delete by index/Id
    • Search between dates

Installation

  • You can search for 'MongoCrud' in NuGet Pakage Manager in Visual Studio.
  • Or you can use .NET CLI
    dotnet add package MongoCrud
    

Usage

Employee Model

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

public  class Employee
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId Id { get; set; }

    public string Name { get; set; }

    public DateTime Birthday { get; set; }
}

Open connection to MongoDB (using statement)

using MongoDB;
string connectionString = "mongodb://localhost:27017";
string databaseName = "EmployeeDB";

using (Crud db = new Crud(connectionString, databaseName))
{
    // do your things here ...
}


Insert Employee data

var emp = new Employee()
{
    Name = "Jone Doe",
    EmpID = 1000,
    Birthday = Convert.ToDateTime("1981-04-13")
};
await db.InsertRecord("Employee", emp);


Insert unique record

var emp = new Employee()
{
    Name = "Jone Doe",
    EmpID = 1000, // Unique ID
    Birthday = Convert.ToDateTime("1981-04-13")
};
await db.InsertUniqRecord("Employee", emp, "EmpID");

Load all records of a collection

var rec = await db.LoadRecords<Employee>("Employee");

Load records by index

var rec = db.LoadRecordByIndex<Employee>("Employee", "Name", "Jone Doe");

Load one record by index

var rec = db.LoadOneRecordByIndex<Employee>("Employee", "Name", "Jone Doe");

Load a record by Id

First, we have to get ObjectId before doing this. To get an ObjectId you can use any method as shown in above.

ObjectId ObjectID = new ObjectId("6366675caf5305273398cfbd");
var rec = db.LoadRecordById<Employee>("Employee", ObjectID);

Search case

Below example will display all records from Employee collection, which Name starts from 'J'

var rec = db.SearchCase<Employee>("Employee", "Name", "J");

Search between two dates

This example will display Employees who has birthday between selected dates.

DateTime startDate = Convert.ToDateTime("1980-04-20");
DateTime endDate = Convert.ToDateTime("1990-04-20");

var rec = await db.LoadBetweenDates<Employee>("Employee", "Birthday", startDate, endDate);

Delete all records by index

This will delete all records where EmpID is, '1000'. However, if EmpID is unique this will also delete a single record.

db.DeleteRecordByIndex<Employee>("Employee", "EmpID", "1000");

Delete a record

To delete a record we have to get ObjectId

db.DeleteRecord<Employee>("Employee", ObjectID);

Updating a record

First we need to load record, and then update.

var oneRec = db.LoadRecordById<Employee>("Employee", ObjectID);
oneRec.Id = ObjectID; // or, oneRec.Id = oneRec.Id;
oneRec.Name = "Jone Doe Smith"

This will update Employee Name.

db.UpsertRecord("Employee", oneRec.Id, oneRec);

Bonus configuration for database connection.

Create a new class name, dbConn

namespace MongoDB;
public class dbConn
{
    public static string connString = "mongodb://localhost:27017";
    public static string dbName = "EmployeeDB";
}

And then you can run CRUD operations, as below

using MongoDB;

using (Crud db = new Crud(dbConn.connString, dbConn.dbName))
{
    // do your things here .... 
}

A full example for insert

using MongoDB;

public class MyProject
{
    public void InsertData()
    {
        using (Crud db = new Crud(dbConn.connString, dbConn.dbName))
        {
            var emp = new Employee()
            {
                Name = "Jone Doe",
                EmpID = 1000, // Unique ID
                Birthday = Convert.ToDateTime("1981-04-13")
            };
            await db.InsertUniqRecord("Employee", emp, "EmpID");
        }
    }
}

❤️😍

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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.2.1 236 3/13/2023
1.2.0 202 3/11/2023
1.1.3 217 2/23/2023
1.1.2.1 273 1/10/2023
1.1.2 263 1/10/2023
1.1.1 264 1/10/2023
1.1.0 317 11/14/2022
1.0.3 302 11/9/2022
1.0.2 300 11/9/2022
1.0.1 340 11/5/2022
1.0.0.1 382 11/5/2022
1.0.0 347 11/5/2022