Thursday, April 16, 2020

MongoDB with C#

MongoDB C# driver can be used to connect to MongoDB from C#.

I am starting a Console Application to start with and added MongoDB driver.




using MongoDB.Driver;
using System;

namespace MongoDBConsoleShalvin
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "mongodb://localhost:27017"; 
            MongoClient client = new MongoClient(connectionString);

            Console.WriteLine("List of databases");
          
            foreach (var item in client.ListDatabases().ToList())
            {
                Console.WriteLine(item);
            }   
            Console.ReadLine();
        }
    }
}


MongoClient class can be used to connect to MongoDB.



Listing Collections

 
using System;
using System.Linq;
using MongoDB.Driver;

namespace MongoListCollections
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "mongodb://localhost:27017";
            MongoClient client = new MongoClient(connectionString);

            var db = client.GetDatabase("CM");
            foreach (var item in db.ListCollections().ToList())
            {
                Console.WriteLine(item);
            }
            
            Console.ReadLine();
        }
    }
}



Insert Document
string connectionString = "mongodb://localhost:27017";
MongoClient client = new MongoClient(connectionString);

var db = client.GetDatabase("CM");

var collection = db.GetCollection<BsonDocument>("Contacts");

var document = new BsonDocument
{
    { "name", "Shalvin" },
    { "location", "Kochi" },
};

collection.InsertOne(document);
Console.WriteLine("Record Inserted");

Insert with Class
using MongoDB.Driver;
using MongoDB.Bson;
..
string connectionString = "mongodb://localhost:27017";
MongoClient client = new MongoClient(connectionString);

var db = client.GetDatabase("CM");

var collection = db.GetCollection<BsonDocument>("Contacts");

Contact c = new Contact { Name = "Shalvin P D", Location = "Kochi" };
            
collection.InsertOne(c.ToBsonDocument());
Console.WriteLine("Record Inserted");

}

    
public class Contact
{
    public string Name { get; set; }
    public string Location { get; set; }
}


InsertMany

using MongoDB.Driver;
using System;
using MongoDB.Bson;
...

string connectionString = "mongodb://localhost:27017";
MongoClient client = new MongoClient(connectionString);

var db = client.GetDatabase("CM");

var collection = db.GetCollection<BsonDocument>("Contacts");

var document =  new List<BsonDocument>
{ new BsonDocument
        {   { "name", "Jacob" },
        { "location", "Chitoor" }
    },
new BsonDocument
    {   { "name", "Plaito" },
        { "location", "Pachalam" }
    }
};

collection.InsertMany(document);
Console.WriteLine("Record Inserted");

List All Documents
using MongoDB.Bson;
using MongoDB.Driver;
..
string connectionString = "mongodb://localhost:27017";
MongoClient client = new MongoClient(connectionString);

var db = client.GetDatabase("CM");
var collection = db.GetCollection<BsonDocument>("Contacts");

var documents = collection.Find(new BsonDocument()).ToList();

foreach (var item in documents)
{
    Console.WriteLine(item);
}

Console.ReadLine();
}

Filter Documents
using MongoDB.Bson;
using MongoDB.Driver;  
..            
string connectionString = "mongodb://localhost:27017";
MongoClient client = new MongoClient(connectionString);

var db = client.GetDatabase("CM");
var collection = db.GetCollection<BsonDocument>("Contacts");

var filter = Builders<BsonDocument>.Filter.Eq("name", "Shalvin");

var documents = collection.Find(filter).ToList();

foreach (var item in documents)
{
    Console.WriteLine(item);
}

Console.ReadLine();

No comments:

Post a Comment