[FSF Associate Member] View Annang Sowah's profile on LinkedIn

Monday, 25 July 2016

MongoDB, BigData and Mungo Park :)
Setup, Start, Query and Stop MongoDB

Hello Folks,
Per my exposure and excitement in relation to Big Data and its utilization in industry and programming especially, I have decided to share a couple of blog postings about same. 

I will focus on MongoDB considering its amazing prospect as one of the leading NoSQL databases in recent years in the Big Data stack of "databases".
The NoSQL regime of data storage has enjoyed massive adoption as a result of its scalability, speedy data retrieval and cloud-ready features it has on offer.
MongoDB, to start with, is a document-based database. It stores data modeled textually according to a format which doesn't even need to be pre-defined.

Dont forget to download  and install your mondgo db binaries from the mongo folks online.

I will start today with the basic setup of the MongoDB in a Windows 10 environment.

First of all, lets create a Windows Service that will enable us start the MongoDB daemon processes with ease.

1. Create a configuration file to host your data and log files. 
You can label the file mongod.cfg and put the content below therein

    destination: file
    path: D:\Tech\DB\mongodb\data\log\mongod.log
    dbPath: D:\Tech\DB\mongodb\data\db

2. We will now proceed to create the windows service called MongoDB.
Take note of the location of your mongod.cfg file and also your install location of MongoDB - mine is C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe.

The syntax for the service creation is 

sc.exe create SERVICE_NAME binPath= "MONGOD_LOCATION --service --config=\"CONFIG_FILE_LOCATION\"" DisplayName= "SERVICE_NAME_ALIAS" start= "auto"

The actual command, as executed on my PC, is as shown below. Note: Run this command as an administrator(open cmd prompt as an administrator)

sc.exe create MongoDB binPath= "C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe --service --config=\"D:\Tech\DB\mongodb\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

3. You are now be ready to start the MongoDB service.

Run net start MongoDB on command line.

4. Start the MongoDB shell to kickstart your running of  "nosql" queries.

Note: Please put the Mongo bin folder(C:\Program Files\MongoDB\Server\3.2\bin) on the system path or as an environment variable for Windows OS. This enables you to access the various executable files easily on the command line with no need of typing the full path.

Run mongo

The mongo shell should be ready to accept your queries.

5. Now query the mongo database.
  • to display the available databases (only ones with data get shown ), type show dbs
  • to use a particular database, type use database_name
  • to see the current database in use, type db

Lets try some data manipulation: Insert my name in the new database sampledb. 
type  db.sampledb.insert({"name":"Joseph Annang Sowah"}).
This creates a new collection to contain my name ;)

6. Lets create a database called softwareSolutions and insert the details of software creators in a collection(pseudo table) called creators. 

NB. databases and collections are created automatically with the use of the use and insert statements

7. Multiple sets of data can be inserted by separating brackets({}) with commas(,) as shown below

8. Lets display the data we have so far in our softwareSolutions mongo database.
This can be done crudely using the find() function.

To have the display formatted, one can attach pretty() to the earlier command
9. Since we have a duplication of Cagatay Civici, let have one deleted. This done by specifically removing the objectId of one of the Cagatay Civici entries.

Now lets re-display the content of the creators collection.

10. Lets quit the mongo shell. To quit the shell, type exit

11. Finally, after quiting the mongo shell, lets stop the MongoDB windows service.
    Command is net stop MongoDB

Bye, thanks for your time.

...and take note, the good old Mungo Park has nothing to to with MongoDB, but about his adventures and exploits you can follow this link :)