GithubHelp home page GithubHelp logo

skiddow / mongocrud Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 6.49 MB

Simple C# class for managing MongoDB collections

License: MIT License

C# 100.00%
csharp dotnet dotnet6 mongodb nosql-database mongocrud mongodb-crud

mongocrud's Introduction

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");
        }
    }
}

โค๏ธ๐Ÿ˜

mongocrud's People

Contributors

skiddow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.