NoSQL, MongoDB Install, Lotus Notes, and CouchDB

The article below discusses the NoSQL movement and derived technologies such as MongoDB. Installation steps are also discussed in detail.

1. Introduction / Overview

In the current software industry that work around design patterns and OOPs there is being a constant battle in converting the data from the database (RDBMs like MS Sql Server/Oracle…etc) into the objects in the object graph and vice versa. For simpler object models this is not a problem but when the model becomes complex it becomes too complicated.

2. Description

As more and more complication gets added into this conversion of data people started thinking why do I need to always keep my data in the database, why can’t I save it in a non-relational manner, is it necessary for me to go in the relational way. This then lead to a movement which started promoting loosely defined classes of non relational data stores. That movement then got the name NoSQL.

These NoSQL data stores usually avoid joins, may not require fixed table schemas and typically scale horizontally. As they don’t have joins or fixed table schemas they provide weak consistency guarantees and transactions are always restricted to single data items.

Note:- In case if we need ACID guarantees for our application using NoSQL then we can implement the necessary logics in the middle layer. But if it becomes too complex then we may need to stop with NoSQL and go with the RDBMs approach.

As this movement started gaining more support, a lot of NoSQL data stores came to the market. These data store are then categorized based on the way they are implemented. The following lists some of the categories and the NoSQL implementations

Document Store
• Lotus Notes
• CouchDb
• MongoDb

Graph
• Neo4j
• AllegroGraph

Key/value store on disk
• Tuple space
• Memcachedb
• Redis
• SimpleDb

Key/Value cache in RAM
• Memchaced
• Velocity
• Redis

Eventually consistent key value store
• Dynamo
• Cassandra
• Project Voldemort

Key-value stores implementing Paxos
• Keyspace

Object database
• Db4o
• InterSystems Caché
• Objectivity/DB
• ZODB

Note:- The above table is not the complete list and it keeps on growing. I have just highlighted some of the data stores.

From these implementations we will now take MongoDb and see how we can work it.

MongoDb

MongoDb is an open source document based data store and it combines the features of Key-Value data stores which are fast and highly scalable and RDBMSes which provides rich querying capabilities. It is fully written in C++ and is scalable.

The data in mongodb are stored as BSON documents which is nothing but binary en- coded serialization of JSON-like documents. BSON, like JSON, supports the embedding of objects and arrays within other objects and arrays. More details on the BSON specification can be obtained from www.bsonspec.org.

In MongoDB we can have one more database and every database will have one or more collections. Here collections are similar to tables in RDBMSes. Each collection will be a collection of documents.

Installation

MongoDb is available both in pre-built distribution as well as in source and if available for the following OS Linux, OS X and Windows. Installation of MongoDb using the built distribution is very simple and we will follow it.

The first step in the installation process is to download the distribution from http://www.mongodb.org/display/DOCS/Downloads. Once downloaded, we then need to unzip it in a folder in the harddisk. We then need to create a folder for MongoDb to store the data files. This folder should have read/write and directory creation permission for Mongodb. By default mongodb will store the data in [The drive when the distribution is extracted]\data\db. This directory has to be created manually and required permission should be given. We can also specify a different directory by using the --dbpath flag when starting mongodb server. Once these steps are completed the installation is complete. The following figure displays the installation structure
Figure 1


The following table describes the various applications that are available.

Application Description
--------------------------------------------------------------------------------
Mongo.exe The database shell
Mongod.exe The core database server
Mongos.exe The auto sharding process
Mongodump.exe The database dump utility
Mongorestore.exe The database restore utility
Mongoexport.exe The database export utility
Mongoimport.exe The database import utility

Starting and stopping MongoDB

Starting

MongoDB can run as a standard program from the command line. In its simplest form with all the default settings all we have to do is to just mongod from the command prompt and the server will be started.
Figure 2


Note:- by default mongodb server will be listening on port 27017 so make sure that the windows firewall is configured to allow that port.

If we want to make mongodb look for a different directory for e.g e:\mymongodb for storing the data store then we need to start mongodb using the –dbpath option as follows:

<your installation drive and folder>/bin/mongod –-dbpath e:\mymongodb

If we want to make mongodb listen on different port other than the default one then we can use the –port option:

<your installation drive and folder>/bin/mongod –port 1001

Again make sure that the firewall is configured to allow this port. We can also use –help option to get a list of all the allowed options The figure figure-4 lists the available list of options.

Once the server is started we can verify the server by connecting to it from the mongo console. In order to do that open another command window and go to the installation folder and type mongo as shown in figure 3
Figure-3


Figure 4



Stopping

Once the server is started we can stop by typing CTRL-C from the command prompt where the server is started or by calling the database method shutdownServer from the mongo console as shown in figure 5

Figure 5 Mongo console


Once the method is called the server will start to shutdown as shown in figure 6
Figure 6 mongod server console


Note: we can call shutdownServer only if we are within the admin database so go to the admin database (by calling use admin from within the mongo console) before calling shutdownServer.

Using mongo console
The mongo console is another utility that can be used to work the MongoDB database from the command prompt or also called shell. This utility can be used to do any operation on the MongoDB data store from the command prompt. We have already seen how we can use the mongo console to initiate the shutting down of the server.

If we have started mongo server with the default options then we can connect to the server by just typing mongo from the command prompt as follows
<your installation drive and folder>/bin/mongo
If we have started mongo server with a different port for e.g 1001 then we need to specify them using the –port option as follows
<your installation drive and folder>/bin/mongo –port 1001
By using the –help option we will a list of all the options available

Figure 7


We can get a list of the available database by using the command show dbs from within the mongo console as shown below

Figure 8


Once we know which database to work with we can use the ‘use’ command to select a database and start working with it

Figure 9


Note :- If MongoDb is started in secure mode by using –auth setting then we can work with the database only if we have access to it and only after we successfully logged into the database.

From with in the console we can type db.help() to see a list of all the options that are available to work with the database.

Figure 10



Note: All the method calls are case sensitive db.shutDownServer() and db.shutdownServer() are both different so ensure that you use proper case with calling the method.

Security

The current version of MongoDB only supports very basic security. One can authenticate a username and password only in the context of a particular database. Once authenticated, the user will have full read and write access to that database. If the user has only read only access then he will have only read access to that database. The admin database is a special one if you have access to the admin database then you can work with any database. So it is always advisable to restrict access to this database.

If we want to use this secure access then we need to start mongod with –auth option
<your installation drive and folder>/bin/mongod –auth

Once mongod is started with this option only after the user is authenticated he can work with any database from the console. If not he will get authentication exception as shown in figure below

Figure 11


We can login into a database by calling the db.auth method from the console and passing in the username and password. Before calling the auth method we need to make the database as the current database by calling use dbname from the mongo console. The following figure shows that in which we have made admin as the current database and logged into it.

Figure 12


A return value of 1 says that the login is successful.

We can add a user to a database by calling the method addUser by passing the required user name, password. The following example creates a user “user1” with password “password1” in the database BlogEngine
>use BlogEngine
>db.addUser (“user1”,” password1”)
In case if you want to give only read-only access then we can pass in the third parameter as true which will give only read only access to the database to the user.
>use BlogEngine
>db.addUser (“user1”,” password1”, true)
If we want to update the password then we can call addUser by passing the username to update and the updated password. The add user method when called will check for the user name if the user is not found then it will create a new user or else it will update the password of the user. So we need to be careful when calling addUser method
>use BlogEngine
>db.addUser (“user1”,” password123”)
The above command will update the password of the user “user1” to password123.

All the authentication information is stored in each database's system.users collection. So we can get a list of all the users within a collection by listing out all the data with this collection. The following figure lists the users within the admin database

Figure 13


We can remove a user by calling the remove method on the users collection and passing in the name of the user. The following command will remove the user “user1” from the BlogEngine database
>use BlogEngine
>db.system.users.remove ({user: “user1”})

The default option and recommended one for implementing security in MongoDB is to run the Mongo database in a trusted environment, with no security and authentication. And then ensure only trusted machines can access database TCP ports.

By prim s   Popularity  (4384 Views)