MongoCrud is a simple c# class for MongoDB CRUD operations.
- Create, read, update and delete documents
- Create unique records
- Case-Insensitive search
- Read by index/Id
- Delete by index/Id
- Search between dates
- You can search for 'MongoCrud' in NuGet Pakage Manager in Visual Studio.
- Or you can use .NET CLI
dotnet add package MongoCrud
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; }
}
using MongoDB;
string connectionString = "mongodb://localhost:27017";
string databaseName = "EmployeeDB";
using (Crud db = new Crud(connectionString, databaseName))
{
// do your things here ...
}
var emp = new Employee()
{
Name = "Jone Doe",
EmpID = 1000,
Birthday = Convert.ToDateTime("1981-04-13")
};
await db.InsertRecord("Employee", emp);
var emp = new Employee()
{
Name = "Jone Doe",
EmpID = 1000, // Unique ID
Birthday = Convert.ToDateTime("1981-04-13")
};
await db.InsertUniqRecord("Employee", emp, "EmpID");
var rec = await db.LoadRecords<Employee>("Employee");
var rec = db.LoadRecordByIndex<Employee>("Employee", "Name", "Jone Doe");
var rec = db.LoadOneRecordByIndex<Employee>("Employee", "Name", "Jone Doe");
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);
Below example will display all records from Employee collection, which Name starts from 'J'
var rec = db.SearchCase<Employee>("Employee", "Name", "J");
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);
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");
To delete a record we have to get ObjectId
db.DeleteRecord<Employee>("Employee", ObjectID);
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);
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 ....
}
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");
}
}
}