Barış Kısır

Senior Software Developer

Navigation
 » Home
 » RSS

MongoDB Basics for .NET

26 Jun 2017 » csharp, sql

What is MongoDB?

MongoDB is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas. MongoDB is developed by MongoDB Inc. and is free and open-source, published under a combination of the GNU Affero General Public License and the Apache License.

Installing MongoDB on Windows

https://www.mongodb.com/download-center#community

// After you installed MongoDB, locate "C:\Program Files\MongoDB\Server\3.4\bin" on cmd.
cd "C:\Program Files\MongoDB\Server\3.4\bin"
// Create folder for db files -> C:/data/db
mkdir "\data\db"
// Start server on cmd, let it stay opened.
mongod


MongoDB Client for Windows - Robomongo

https://robomongo.org/download

In MongoDB, we have collections instead of tables

Connect to localhost on 27017 port and create database as “hr”, then create collection as “employee” on hr database.

You can insert any JSON object to this collection I got a sample json data here or you can generate by using mockaroo.com


Robomongo


NoSQL

MongoDB Query Documentation

You can convert SQL queries to NoSQL by using querymongo.com


MongoDB Providers for .NET

Install-Package MongoDB.Driver

Install-Package MongoDB.Bson

Install-Package mongocsharpdriver


Employee class

Every object has ObjectId which is generated by MongoDB.

public class Employee
{
    public ObjectId Id { get; set; }
    [BsonElement("name")]
    public string Name { get; set; }
    [BsonElement("surname")]
    public string Surname { get; set; }
    [BsonElement("email")]
    public string Email { get; set; }
}


MongoDBContext class for CRUD operations

public class MongoDBContext
{
    private MongoClient client;
    private MongoServer server;
    private MongoDatabase db;
    public MongoDBContext()
    {
        this.client = new MongoClient("mongodb://127.0.0.1:27017");
        this.server = this.client.GetServer();
        this.db = server.GetDatabase("hr");
    }
    public IEnumerable<Employee> GetEmployee()
    {
        return this.db.GetCollection<Employee>("employee").FindAll();
    }
    public Employee GetEmployee(ObjectId id)
    {
        var result = Query<Employee>.EQ(x => x.Id, id);
        return this.db.GetCollection<Employee>("employee").FindOne(result);
    }
    public IEnumerable<Employee> GetEmployeeByName(string name)
    {
        var result = Query<Employee>.EQ(x => x.Name, name);
        return this.db.GetCollection<Employee>("employee").Find(result);
    }
    public Employee Create(Employee employee)
    {
        this.db.GetCollection<Employee>("employee").Save(employee);
        return employee;
    }
    public void Update(ObjectId id, Employee employee)
    {
        employee.Id = id;
        var result = Query<Employee>.EQ(x => x.Id, id);
        var operation = Update<Employee>.Replace(employee);
        this.db.GetCollection<Employee>("employee").Update(result, operation);
    }
    public void Remove(ObjectId id)
    {
        var result = Query<Employee>.EQ(x => x.Id, id);
        var operation = this.db.GetCollection<Employee>("employee").Remove(result);
    }
}


CRUD Operations

var db = new MongoDBContext();

// Get all employees
var allEmployees = db.GetEmployee();

// Get by id
var employeeById = db.GetEmployee(new ObjectId("595152fd28c7b9be12251719"));

// Get by name
var employeesByName = db.GetEmployeeByName("Alisander");

// Create new employee
var employee = new Employee();
employee.Name = "Kemal";
employee.Surname = "Etikan";
employee.Email = "[email protected]";
db.Create(employee);

// Update existing record
db.Update(new ObjectId("595152fd28c7b9be12251719"), employee);

// Remove record
db.Remove(new ObjectId("595152fd28c7b9be12251719"));


Robomongo-NET


You can download source code from –> Here