Monday, April 27, 2020

Redis with C#

Redis is a Key Value, very fast NoSql Database.

Extract the Window Redis Zip. Inside the 64 folder you can find redis-server and redis-cli. Double click redis-server to start the Redis Server.


Double click the redis-cli to open the Redis Command Line Interface.

set command is used to set a value to a Key. get command is used to get the value of a key.



setnx
 If you issue set command to a key which already has a value then the existing value will be replaced with the new value. setnx command is used to create a value for a key which doesn't exist.

incr and decr

incr command is used to increment a value by 1.  decr is used to decrement a value by 1.





 I have create a Console Application and Set reference to StackExchange.Redis. 






using System;
using StackExchange.Redis;
..
static void Main(string[] args)
{
    IConnectionMultiplexer redisCon = ConnectionMultiplexer.Connect("localhost");
    IDatabase db = redisCon.GetDatabase();
    db.StringSet("MyName", "Shalivn P D");
    Console.WriteLine(db.StringGet("MyName"));
    Console.WriteLine("Hello World!");
}

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();

Thursday, April 9, 2020

MongoDB

MongoDB is a NoSql Document based batabase.

MongoDB stored data inside a Database. A database have Collections. Collections are equivalent to tables in an RDBMS. Collections in turn contains Documents.

MongoDB can be accessed by typing mongo in command line.

help command can be used to list Mongodb commands.


use command can be used to use or create a database.  If the database already exist the MongoDB will switch the context to the database. If the database doesn't exist MongoDB will create a new one.


createCollection() can be use to create a collection. 

db.insert() command  can be used to add a document to the collection. A document is a JSON object that is stored in the database as BSON, a binary format.

>db.Contacts.insert({"name": "Shalvin"}) 





db..find() command is used to select the data in a collection.

> db.Contacts.find()


Though I have specified only the name while inserting the document it automatically adds another field _id which is the primary key.

MongoDB is schema-less. 

> db.Contacts.insert({"name": "Plaito", "location": "Kochi"})





 count() function can be used to retrieve the number of documents in a collection.

> db.Contacts.find().count()







> db.Contacts.find[0]
> db.Contacts.find()[0]
{ "_id" : ObjectId("5e8f1762fadf026099d135e1"), "name" : "Shalvin" }
> db.Contacts.find()[1]
{
        "_id" : ObjectId("5e8f1b0afadf026099d135e3"),
        "name" : "Plaito",
        "location" : "Kochi"
}