Introduction
Databases are the heart of any small or enterprise level software applications. Hence choosing a cost effective, high performance and scalable database is very essential for the success of the project in general and business in particular.
MongoDB is a scalable, high performance open source NoSQL Database.
MongoDB stores data using a flexible document data model that is similar to JSON. Documents contain one or more fields, including arrays, binary data and sub-documents. Fields can vary from document to document. This flexibility allows development teams to evolve the data model rapidly as their application requirements change.
Developers access documents through rich drivers available in all popular programming languages. Documents map naturally to the objects in modern languages, which allows developers to be extremely productive. Typically, there’s no need for an ORM layer.
Features
1)Document-Oriented Storage
JSON (Java Script Object Notation) Document oriented (actually BSON – Binary Script Object Notation). This facilitates dynamic schemas which offer simplicity and power.
Data in MongoDB has a flexible schema. Collections do not enforce document structure. This flexibility gives you data-modeling choices to match your application and its performance requirements.
2)Full Index Support
Index on any attribute, just like you’re used to.
3)Replication & High Availability
Mirror across LANs and WANs for scale and peace of mind.
4)Auto-Sharding
Scale horizontally without compromising functionality.
5)Querying
Rich, document-based queries.
6)Map/Reduce
Flexible aggregation and data processing.
Relational databases save data in tables and rows and our application code hardly ever does that. There is a misalignment of application code objects to tables and rows in database. Hence we end up using some Object Relational Mapping technology such as NHibernate to translate the database structure into application layer objects. This can be avoided using NoSQL database like MongoDB.
In MongoDB there is no schema to define, no tables and no relations between collections of objects. Every document we save in MongoDB could be very simple or quite complex based on the needs of the application layer which makes the life of the developer very easy and helps in writing cleaner code with less effort. Also since there is no schema defined for any document saved in the database it can be extended/changed as needed.
Why should I go for MongoDB?
1)Data Set is Big & Schema is Not Stable
Adding new columns to RDBMS can lock the entire database in some database, or create a major load and performance degradation in other. Usually it happens when table size is larger than 1GB. As MongoDB is schema-less, adding a new field, does not affect old rows (or documents) and will be instant. Other plus is that you do not need a DBA to modify your schema when application changes.
2)NO DBA Assistance
If you don’t have a DBA, and you don’t want to normalize your data and do joins, you should consider MongoDB. MongoDB is great for class persistence, as classes can be serialized to JSON and stored AS IS in MongoDB.
3)Need High Availability (Cloud and Real Life)
Setting replica Set (set of servers that act as Master-Slaves) is easy and fast. Moreover, recovery from a node (or a data center) failure is instant, safe and automatic
4)You Expect a High Write Load
MongoDB by default prefers high insert rate over transaction safety. If you need to load tons of data lines with a low business value for each one, MongoDB should fit.
Setting up MongoDB Server
Download the latest Mongo DB setup from the following location and install. You can download for either 32 bit operating system or 64 bit operating system.
http://www.mongodb.org/downloads
Once you complete the installation of the setup navigate to the installation folder and you will find a bunch of executables and one among them is the Mongo Daemon executable.
Open command prompt with Run as administrator option.
Default location for MongoDB is \data\db
Once the folder exists on the file system then we can start the MongoDB server daemon. In case if we would like to store data in a different location then first create the database and later start MongoDB daemon service.
- I have created a folder called D:\Database\MongoDB
- Navigate to the folder in command prompt.
- Identify the path of the mongod.exe file on your machine
- Start MongoDB server daemon service
Note: Press CTRL + C to shut down the server.
To learn about the additional options type mongod –help | more
You will realize that we have option to write log files and set the verbose. Limit the maximum number of connections and providing all the inputs through a configuration file etc…
Let’s create a sample configuration file as shown below.
And start the service with the help of configuration file as shown below
If you do not want to start and stop the server daemon, you can as well choose to install it as a windows service on the machine.
Use the install option as shown below to install it as a windows service on the machine.
Now we can use net start mongodb and net stop mongodb commands to start and stop it.
Or alternately use the services console to start and stop the service as needed.
Test the connection to MongoDB server
Once the server is started use mongo,exe shell to connect to the server.
Open command prompt and type mongo.exe
It will connect to the server
How to insert a document
db.messages.insert({name:’Kishore’, message: ‘Hello’})
The name of the collection that we are creating in the above document is messages. It is equivalent to our table in RDBMS terminology
Now type db.messages.find()
It will retrieve all the entries in the collection.
To close the shell type exit in the command prompt
RoboMongo
We can also download and install a tool called RoboMongo which will be an UI based client.
Hope you have enjoyed this post and in my next post, I will try to explain the concept of MongoDB Replication with examples.