Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Thursday, November 29, 2012

MongoDB–NoSQL database

Lately I’ve been hearing a lot about so called NoSQL databases, Google has one, the BigTable. These databases are highly optimized for retrieving and saving data. They do not use SQL-like languages for data manipulation, and save data not in tables but in collections under key:value objects.

When writing rich client web applications you often find yourself moving data from the database through server side and to the client while serializing and converting the data to JSON in the process. Because of this the idea of a database that will save data as objects and will on start deliver the data to you in JSON seems very appealing.

After little research on this topic I’ve found the MongoDB database that can be found here. This database is used by number of companies e.g. SAP, Disney, Github, and many others. So, I’ve decided to take it to a test drive.

Installation

So, first of all we need to download the MongoDB from the website. There is not installation, just a zip file that should be extracted into a directory of your choice.

Inside MongoDB\bin directory you will find two files of interest, the mongod.exe and mongo.exe files. mongod file is the one we will use to startup the server, mongo file will is a console to run commands against the server. By default MongoDB will try to save all the databases into c:\data\db directory. If this directory does not exist mongod will fail to start unless we will create it or specify another directory where to save the databases.

To start MongoDB with specific save directory the following command should be initiated:

mongod –dbpath <directory>

After this, the following lines should be displayed:

image

You can also start MongoDB as a windows service. The following command line should be initiated:
mongod –install do not forget to add –dbpath if needed

Actually this is it, we have just installed and started a MongoDB server. Now we can start mongo.exe to run script commands on the database. We can also open web browser and navigate to localhost:28017 to be able see web adminsitration window:

image

Giving it a try

When we start the mongo console it will by default connect to test database, the database does not yet exist, it will be created when we save our first data in some collection. It is important to note, MongoDB does not have tables, it has collections instead. Instead of rows it has documents (objects). To save data we should run the save function on collection of our choice and pass a json object to it. For example, if we need to add a person with it’s first and last name to people collection it should look like this:

image

Now, to get all the documents from a collection we should run find function:

image

You can see we received the object we added to people collection and with it can back an _id field. This field was given to the document by the MongoDB and it represents a unique ID. If we would try to imagine how this function would look in SQL it should look like this:

SELECT * FROM People

You can get documents from collection on condition, for example:

db.people.find({firstName:”Israel”})

will be equals to

SELECT * FROM People WHERE firstName = ‘israel’

And of course you can get only part of the members in documents like so:

db.prople.find({}, {firstName:true})

is as same as

SELECT firstName FROM People

Last but not least for this post: we are free to change database whenever we want, for this we should run the use <database> command. Again, if the database does not yet exist it will not be created right away, it will be created when we save command.

Other options for the console can be printed out if you initiate help command

Conclusion

No-SQL database seems as a great and easy way to save and administer large amounts of data and then retrieve them as object right from the database. At MongoDB website you can find some of the user cases when you should consider using a No-SQL database, page can be found here.

There is also MongoDB ports for various programming languages that can be found here.