• MongoDB-Replication

    REPLICATION

    Replication helps us achieve availability and fault-tolerance. A replica set is a set of mongo nodes that replicate data amongst each other asynchronously. One of the replica sets is primary while the rest of them will be secondary. Writes only happen to the primary. If the primary goes down then an election happens and the new primary comes up. Minimum number of nodes will be 3, since the election requires a majority of the original set. If there were only 2 sets then the remaining one is not a majority and you would not be able to write.

    Read more...


  • Review-CSharp in Depth.

    23 Oct 2013

    I have read the second edition as well. The third edition carries on from there and provides a deeper look in C# 5 and it's key feature async/await. If you want to understand what goes on behind the scenes, this one is for you. Jon Skeet ( yes the famous guy Jon Skeet) has managed to come out with a great book once again.

    Read more...


  • MongoDB-Understanding queries through explain plan

    Understanding the queries we write is very critical and MongoDB does a good job here. Developers will find it easy to understand what the queries are doing and where to look for bottlenecks. Well defined parameters and also well documented ones make life a lot easier. The details have been taken from the mongodb website and presented here for continuity of series.

    Read more...


  • MongoDB-Indexing

    Indexing is one of the most important concepts for any database. Without indexes the mongod process would scan the entire collection and the all the documents it contains to obtain the result of the query. Indexes are defined for the collections and properties as well as sub-fields are supported. Briefly, MongoDB supports the following types of indexes :

    • Single Field Indexes : Think about having an index on a column in RDBMS.
    • Compound Indexes: Think about an having an index on multiple columns in RDBMS.
    • Multikey Indexes: This is unique to MongodB, it references an array and succeeds if there is a match for any value in the array.
    • Geospatial Indexes and Queries : Allows you to index GeoData. I really don't know much about this to comment. MongoDB website is the best source.
    • Text Indexes : For full text search inside a document. Should we use lucene ? Not sure ?
    • Hashed Index: Index on hashed contents of the fields.

    Read more...


  • MongoDB-Ops-Stuff

    In the previous two posts we have seen some basic querying and how to leverage the querying mechanism to get up and running. Now, we are off in the wild world and we also need to some more complicated stuff.

    Creating Indexes The api surface is really smooth with this, allowing us to specify the sort order of the indexes and the manner of building them foreground or background.

    public void CreateIndex()
    {
    var QuestionConnectionHandler = new MongoConnectionHandler<Question>("MongoDBDemo");
    QuestionConnectionHandler.MongoCollection.EnsureIndex( 
                          IndexKeys.Ascending("Difficulty"), IndexOptions.SetBackground(true));
    }
    

    Read more...


  • MongoDB-CSharp-Driver Getting organised

    Having done the inital work for talking to MongoDB we can now create some POCO classes and then do some querying on top of it. As usual, my model is Questions and Users.

    public class Question : MongoEntity
    {
        public string Text { get; set; }
        public string Answer { get; set; }
        public DateTime CreatedOn { get; set; }
        public int Difficulty { get; set; }
    }
    public class User : MongoEntity
    {
        public string Name { get; set; }
        public int Reputation { get; set; }
    }
    

    Read more...


  • MongoDB-CSharp-Driver-Using Linq

    In the previous post we have seen some simple queries. It is time we move onto something more concrete and realistic. There are basically two ways of querying MongoDB with the driver. First, as I showed last time is using LINQ. To use LINQ we need to first move into the Queryable world and then proceed with actual querying. Be careful about pulling all the documents locally and then performing operations on them. What we really want to do is offload all our querying to MongoDB and then only use the results. The driver implements the IQueryable interface and hence we should use it.

    var result = UserConnectionHandler.MongoCollection.AsQueryable()
                                      .Where(u => u.Reputation > reputation);
    

    Read more...


  • MongoDB-CSharp-Driver Getting started

    There are several drivers available for C#. I do not plan to go thorugh all of them here. Since, the official driver now has LINQ(although not complete yet) support, we will go with it. Basic Setup..get the stuff of NuGet. It should put in two dll's in there 1. MongoDB.Bson 2. MongoDB.Driver

    We will get to what does what later. For now assume that we only want to get some data in and out of MongoDB. Let's connect to MongoDB now(the code below is just quick and dirty, we will see a better version later).

    //MongoDB should be running by now, and assuming you have inserted some documents in there
    var client = new MongoClient(@"mongodb://localhost");
    var server = client.GetServer();
    var database = server.GetDatabase("YourDataBaseName");
    var mongoCollection = database.GetCollection("SomeCollectionName");
    //Getting all the documents
    var cursor = mongoCollection.AsQueryable();
    cursor.ForEach(Console.WriteLine);  
    

    Read more...


  • MongoDB-Schema Design

    This is a short post on Schema Design.

    Schema Design in MongoDB

    • Application Driven Schema is the best way to store data in MongoDB
    • MongoDB stores rich documents i.e. store embedded documents , arrays etc..
    • Joins happen at the application level not at the database level.
    • There are no constraints in the database.
    • No support for transactions.
    • Support for Atomic present operations is present.
    • Tip : Embed as much as you can.

    Read more...


  • MongoDB-Query

    Hopefully, the MongoDB set up in the last post is still working. Let's create a database and name it Awesome(just because we can).

    > use Awesome
    switched to db Awesome
    

    Read more...