Search This Blog

Wednesday, 21 May 2014

Baby steps at understanding MongoDB

I have never ventured outside the world of relational databases. So when an opportunity to peak into Big Data arrived, I couldn't let the opportunity go. I decided to start with a no sql database first.
After careful thought and deep analysis over a long long period of  5 minutes, I settled on MongoDB. (ovation please !!)
I decided to first get it installed and configured on my machine. Being a 64 bit windows machine , I downloaded the bundle from the MongoDB website and then followed the installation instructions specified.
With the number of mini customizations I made, running the mongod.exe command would require me passing a number of parameters. So I decided to create a simple batch file for the same.
@echo off 
:this will not display the lines in the bat file on the cmd prompt
set ROOT=E:\work\MongoDB Installation
set dataF=%ROOT%\Data
set logF=%ROOT%\Logs
echo Default file - added by Robin
echo Default location for MongoDB Data is %dataF%
mongod.exe --dbpath "%dataF%" --logpath "%logF%\mongo.log"
If I were to run my batch file:
I looked into the log file:
Sat Jan 11 14:49:13.117 [initandlisten] MongoDB starting : pid=8872 port=27017 
dbpath=E:\work\MongoDB Installation\Data 64-bit host=MsInUs
Sat Jan 11 14:49:13.119 [initandlisten] db version v2.4.9
Sat Jan 11 14:49:13.119 [initandlisten] git version: 52fe0d21959e32a5bdbecdc62057db386e4e029c
Sat Jan 11 14:49:13.119 [initandlisten] build info: windows sys.getwindowsversion(major=6, 
minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
Sat Jan 11 14:49:13.119 [initandlisten] allocator: system
Sat Jan 11 14:49:13.119 [initandlisten] options: { dbpath: "E:\work\MongoDB Installation\Data", 
logpath: "E:\work\MongoDB Installation\Logs\mongo.log" }
Sat Jan 11 14:49:13.155 [initandlisten] journal dir=E:\work\MongoDB Installation\Data\journal
Sat Jan 11 14:49:13.169 [initandlisten] recover : no journal files present, no recovery needed
Sat Jan 11 14:49:13.236 [FileAllocator] allocating new datafile E:\work\MongoDB Installation\Data\local.ns, 
filling with zeroes...
Sat Jan 11 14:49:13.236 [FileAllocator] creating directory E:\work\MongoDB Installation\Data\_tmp
Sat Jan 11 14:49:13.317 [FileAllocator] done allocating datafile E:\work\MongoDB Installation\Data\local.ns,
 size: 16MB,  took 0.063 secs
Sat Jan 11 14:49:13.331 [FileAllocator] allocating new datafile E:\work\MongoDB Installation\Data\local.0, 
filling with zeroes...
Sat Jan 11 14:49:13.539 [FileAllocator] done allocating datafile E:\work\MongoDB Installation\Data\local.0, 
size: 64MB,  took 0.204 secs
Sat Jan 11 14:49:13.542 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760,
 capped: true } ntoreturn:1 keyUpdates:0  reslen:37 338ms
Sat Jan 11 14:49:13.543 [initandlisten] waiting for connections on port 27017
Sat Jan 11 14:49:13.543 [websvr] admin web console waiting for connections on port 28017
I took a snapshot of my data folder: 
Based on the above:
  1. Within the data folder there is the mongod.lock file which is a lock file like that used by other applications.
  2. There is a journal folder. Based on the documentation, it is used to store write operations on disk prior to MongoDB applying them to databases.
  3. The .ns files are namespace files. These hold information about the collections and indexes used.
  4. There is also a .0 file. Sometimes there will be even .1, .2 and so on other files. From the website:
    In the data directory, MongoDB preallocates data files to a particular size, 
    in part to prevent file system fragmentation. MongoDB names the first data file 
    <databasename>.0, the next <databasename>.1, etc. The first file mongod 
    allocates is 64 megabytes, the next 128 megabytes, and so on, up to 2 gigabytes, at 
    which point all subsequent files are 2 gigabytes. The data files include files with 
    allocated space but that hold no data. mongod may allocate a 1 gigabyte data file 
    that may be 90% empty. For most larger databases, unused allocated space is small 
    compared to the database.
    
    As I have no database created, the file was named as local.0 and so on. 
In the next post I shall start on actually using the database.

2 comments: