Friday, October 17, 2014

MongoDB Installation and running


This post will discuss about the installation of MongoDB, how to create config files, how to start MongoDB.

For installation of the MongoDB, kindly go to the following URL and select the appropriate installation file, based on the operating system which you are using:

http://docs.mongodb.org/manual/installation/

In my case, I am using windows 8 which is 64 bit, but my mongodb version is 32bits.

I will advise to use 32 bit installation of MongoDB because the drivers developed in the languages such as Python etc work very well with 32 bits version. There are lot of issues encountered when 64 bit version of Python is used. Also, use Python 2.x version, not 3.x

Once the installation is complete, run the setup, install mongodb in the directory. I have chosen C: directory for the same.

Starting MongoDB:

MongoDB can be started as windows service. But before that, we need to create --dbpath file,conf file and the log file.

Infact, the best way is to create dbpath and logpath in the config file and run the config file while starting the mongodb instance, which I will explain.



Create a config file, mongodb.conf, put it in the folder C:\wamp\bin\mongodb\mongodb_win32\conf,which might be different for your machine. Just make sure that conf file is residing where the mongodb resides inside the \bin directory. Also, kindly take care of the path, for windows it is '\' and for unix it would be '/'

Add the following lines in the config file and name it as mongodb.conf:

# mongodb.conf

# data lives here
dbpath = C:\wamp\bin\mongodb\mongodb_win32\data\db\

# where to log
logpath = C:\wamp\bin\mongodb\mongodb_win32\logs\mongodb.log
logappend=true

# only run on localhost for development
bind_ip = 127.0.0.1                                                             

port = 27017
rest = true

To run the mongodb instance from the cmd, make sure to open the cmd in administrative, for linux and unix, the mode should be super user.

Now, open the command prompt as administrator and do the following:




The mongod instance is up and running.

Now, open another command prompt as administrator and do the following:


Here, we have use mongo command to connect to the database, we are connected to the test database.


By default, mongo looks for a database server listening on port 27017 on the localhost interface. To connect to a server on a different port or interface, use the --port and --host options.

The mongo shell  session will use the test database by default. At any time, issue the 'db' operation at the mongo to report the name of the current database:

To get the list of databases, use 'show dbs'



To use a database, issue use db command, where db is the name of the database, as show in the screen shot below:



Now we are connected to the students database.

As mongoDB is a json related document,A manual:database holds a set of collections. A manual:collection holds a set of documents. A manual:document is a set of key-value pairs.

In order to find the collections of a database, issue 'show collections' operation:

The collection present in the database 'students' is 'grades'. It is using this collection that we will perform all the operations on the database. 

Some basic operations using mongoDB queries, such as finding data inside the collection, counting the number of documents in a collections:

To find the data, we use the operation db.collection.find()

For our example, we will use db.grades.find()




We can see the output in the key:value pairs in the documents returned by the command, "student_id" is key whose values are 0,1,2 etc.

To find the number of in a collection, use db.collection.count()




The number of documents in the grades collection is 600.

In the next post, I will show some more operations such as finding specific document, and how to use aggregation framework to sum,average data and also, I will show you how to write file using pymongo, a driver available in python language.

References:

[1] http://docs.mongodb.org/manual/reference/method/db.collection.count/

[2] https://university.mongodb.com/


No comments: