Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Getting Started with NoSQL and MongoDB

, 26 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Getting Started with NoSQL and MongoDB

Journey to NoSQL Land

Recently, I decided to start playing with NoSQL and to break away from my traditional rational database model. The two biggest benefits for using NoSQL, as opposed 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

JonB3232
Software Developer Symantec
United States United States
I've been developing for the last few years professionally and recreationally. I'm a Software Engineer for Symantec where I develop for a few of their mainline products. My experience includes development in many spectrums of .NET including MVC, SharePoint and Azure development; as well as additional languages such as Java, Perl, HTML5 and JavaScript. I have two Microsoft certifications; one, for development in SharePoint 2010 (MCTS), and the other for programming in HTML5, JavaScript and CSS3 (MS). My interests are in cloud development and mobile development. Feel free to reach out to me for questions and/or advice.
Follow on   Google+   LinkedIn

Comments and Discussions

 
Questionnice PinprofessionalBillW3314-Mar-14 3:21 
QuestionProblem with the code tags in the article PinmemberKevin Bewley3-Mar-14 3:23 
AnswerRe: Problem with the code tags in the article PinmemberJonB32324-Mar-14 2:38 
GeneralRe: Problem with the code tags in the article PinmemberKevin Bewley4-Apr-14 0:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 26 Feb 2014
Article Copyright 2014 by JonB3232
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid