Tuesday, February 25, 2014

Getting Started with NoSQL and MongoDB

Journey to NoSQL Land

Recently, I decided to starting playing with NoSQL and to break away from my traditional rational database model. The two biggest benefits for using NoSQL, oppose to RDBMS, are increased scalability and increased performance. There are many reasons that contribute to these benefits, which are outside the scope of this article, but here's a good place to start:

MongoDB

MongoDB is a NoSQL document-oriented database type system. Why did I choose MongoDB over the other NoSQL systems available? Mainly because it's considered the most popular NoSQL database system and it integrates with .NET really well (which is right up my alley).


Installing MongoDB for Windows

To begin, you can download the MongoDB Windows distribution from their download site - it will download as a .zip package. Make your life easier by extracting the folder to your desired location and rename the folder to something simple. E.g. C:\mongodb\
Extract zip package to desired location
Next, let's set up our folder structure to store the MongoDB databases by creating a directory named "data", and a subdirectory named "db". E.g. C:\data\db\
Create a directory name "data" and a subdirectory named "db"
Congrats, you have installed MongoDB and have began your journey to NoSQL Land! Now let's start using MongoDB.


Getting Started with MongoDB

First, we need to create a connection that will be used between the MongoDB client and the MongoDB service to use the MongoDB databases. This can be accomplished by opening the command prompt and going to the "bin" folder from the extracted MongoDB zip package, and then executing the MongoDB service.
 cd C:\mongodb\bin
 mongod.exe -dbpath "C:\data\db"
Your firewall will block the connection; although, you should be prompted for access in which you can grant access for Private networks.
Grant access for Private networks
This will open the corresponding port needed to make the connection available for a MongoDB client. The connection is ready when you see the following line:
 [initandlisten] waiting for connections on port 27017
Connection is ready for a MongoDB client
We are ready to create a database and add some content! Open the MongoDB client shell with the following command:
 mongo.exe
Once the shell opens, a connection has been made between the shell client and the service as you can see below in the highlighted text:
Connection made between client and service
To familiarize yourself with some of the commands, type "help" to see a list of commands. Type "shows dbs" to see a list of your databases (by default there should be a test and local database). Let's add some content to the test database with the following command:
 use test 
 db.test.save({ someStringField: "String value", someIntField: 23, someObjectField: {objectField1: "Some value", objectField2: "Some value again"}})
Now let's check to see if the document was stored by executing the following command:
 db.test.find() 
The content entered previously should be returned along with the document's corresponding id. Below is a screenshot of the expected results and some other screenshots of other queries that you can execute using the MongoDB shell.
Example of executing MongoDB shell commands
Second example of executing MongoDB shell commands
That's it! We've officially made it to NoSQL land using MongoDB.

Creating the MongoDB Windows Service

One more thing I want to cover is creating the MongoDB Windows service. This eliminates the step of executing the MongoDB service (mongod.exe command), creating the listener for the service.
To create the MongoDB Windows service, we have to create a subdirectory named "log" in the MongoDB directory we extracted from the beginning. Afterwards, we have to create a config file that points to the log folder path. This can be done in the command prompt with the following commands:
 md c:\mongodb\log 
 echo logpath=c:\mongodb\log\mongo.log -logappend > c:\mongodb\mongod.cfg 
Create "log" subdirectory and config file
Now we can create and start our service with the following commands within the MongoDB "bin" directory:
 cd c:\mongodb\bin 
 mongod.exe -dbpath "c:\data\db" -config "c:\mongodb\mongod.cfg" -logpath "c:\mongodb\log\mondo.log" -install 
 net start MongoDB 
Create and start the MongoDB Windows service
Verify the MongoDB service exists and was started in services.msc. If everything was configured correctly, you should be able to execute commands from the MongoDB client without issues and there should be some logging created in the "log" folder.
Mongo DB Windows service created and started
Next, I will be using MongoDB's GridFS model for storing large documents such as pictures, movies, audio etc. similar to a blob storage.

Happy Coding!!

Resources