MongoDB NoSQL Solution: Advantages and Disadvantages

Wednesday Feb 18th 2015 by Uma Narayanan

MongoDB helps develop applications faster because it uses tables; stored procedures are no longer required.


The NoSQL database is a way of storing data in means other than the relational database. The NoSQL varieties are further categorized into key-value, column, graph, and document, to name a few.

MongoDB is an open source software that falls under the "Document" category. MongoDB helps develop applications faster because it uses tables; stored procedures are no longer required. It offers advantage to developers because earlier the tables were to be translated to the object model before they could be used in the application. Now, the object model and the stored data have the same structure, similar to the JSON format, called BSON. MongoDB also supports scalability and provides various options to handle data consistency. We will see these features in more detail.


In MongoDB, data or records are called documents and the documents are stored in a binary JSON (BSON) format. Records—documents—are further organized in collections. However, in the same collection, the schema of one document can be different from the other. Hence, flexible schema are supported. Because developers are familiar with JSON formats, using BSON is simpler.

Nested data structures and array formats are also supported with MongoDB. To implement the same in a relational DB would mean multiple tables with foreign key relationship. It also supports typed data, such as integer, string, date time, double, and so forth.

It supports indexing and querying. A query can vary from key-value, range, or geospatial to aggregation framework queries. Indexes enhance performance when querying the data. Indexes can be declared on unique/single and multiple fields. An index also can be on fields from a nested structure. As with SQL, it also supports verifying the execution plan to optimize query performance.

MongoDB provides a replica set: a failover mechanism. There is only one Primary database that allows a write operation and multiple secondary servers only for read operations. A minimum of three servers is required for a replica set: Primary, Secondary and, Arbiter. Arbiter doesn't store any data; it's only used during failover to decide which server will be the next primary server.

MongoDB with .NET

To use MongoDB with .NET, let's first start with installing and setting up the MongoDB server.


Download the installation bits from There are different installable versions available, based on the operating system you are using. Unzip the downloaded files to a folder location.

In the extracted location in the bin folder, there are few executables (exes); out of them, "mongod" is the one that starts the MongoDB server and "mongo" is the shell command. Create a folder(s) for storing the files, for example, the data\db folder created to store document(s). Start the DB server by using the following command in the command shell:

>mongod --dbpath "f:\mongodb\data\db"

In a production scenario, we would need the mongo server to run continuously and in a Windows environment MongoDB server can run continuously as a Windows service. Create a configuration file specifying the port number (port), database path (dbpath), and the log location (path).

Here's an example:

   port: 14000
   dbPath: F:\MongoDB\Data\DBFromService
   destination: file
   path: F:\MongoDB\Data\Log\mongodb.log
   logAppend: true

In the command prompt, run the following command:

>Mongod -f "path of config file" --install

This creates a service that can be started by using the next command:

>Net start mongodb

This sets up the mongodb server and it's ready to accept connections.

Connect to a MongoDB Server from C# Code

To create a connection to the database, you need a connection string and the database name, for example:

1. public MongoDatabase mDB;
2. MongoClient mClient =
   new MongoClient("mongodb://localhost:14000");
3. var mServer = mClient.GetServer();
4. mDB = mServer.GetDatabase("studentDB");

Line 2 specifies the connection string to connect to the mongodb server. Line 3 gets the "server" object and Line 4 gets the database named "studentDB". One thing to remember is in MongoDB databases are namespaces; if studentDB doesn't exist, it creates it.

Create a Schema Definition

In this example, we will do simple insert operations and display the data on the UI. First, we create a model that represents the schema in the database.

 1. public class Student
 2. {
 3.    [BsonRepresentation(BsonType.ObjectId)]
 4.    public string Id { get; set; }
 5.    public string FirstName { get; set; }
 6.    public string LastName { get; set; }
 7.    public string[] Subjects { get; set;}

 8.    public Student()
 9.    { }
11.    public Student(string firstName,
          string lastName, string subjects)
12.    {
13.       FirstName = firstName;
14.       LastName = lastName;
15.       Subjects = (subjects ??
16.    }
17. }

MongoDocument requires an "Id" field to represent a record. Id field should be unique. The user can choose to explicitly specify a value; however, they need to ensure that it's a unique value. The DB also can automatically create on its unique key. In the preceding class, "Id" is decorated with BsonType.ObjectId; in other words, the user needn't pass any value to this field. MongoDB will use this field to assign an auto generated value and use this field as unique key.

Insert and Retrieve Information

Inserting data to the database is simplified, as shown in the following code:

1. MongoCollection<Student> Students =
2. Student student = new
3. Students.Insert(student);

Line 1 gets a collection object named "students". Line 2 creates a "student" object and Line 3 adds the document to the collection.

Use the FindAll method to retrieve all the documents in that collection.

var coll = Students.FindAll();

The retrieved collection can be bound to the UI to display the data. Queries can be used to filter the records while retrieving the data.


MongoDB enables horizontal scalability by using a technique called sharding. Sharding distributes the data across physical partitions to overcome the hardware limitations. The data is automatically balanced in the clusters.

It also provides ACID properties at the document level as in the case of relational databases.

It supports replica sets; in other words, a failover mechanism is automatically handled. If the primary server goes down, the secondary server becomes the primary automatically, without any human intervention.

It supports the common authentication mechanisms, such as LDAP, AD, and certificates. Users can connect to MongoDB over SSL and the data can be encrypted.

MongoDB can be a cost effective solution because improves flexibility and reduces cost on hardware and storage.


Given all the advantages, MongoDB is definitely a promising solution. It may be very useful in creating applications like bug tracking, discussion forums, advertisements, and the like. However, it may not be able to cater to all the needs that a relational database supports. Joins are not possible; this is an advantage in relational DBs. It requires proper analysis before making a decision.


Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved