online learning, blog, anil singh

«

»

Feb 10

MongoDB Basics Primer Series - Step 1

 

This primer series on MongoDB is aimed to be an authoritative, step-by-step reference guide for a non-starter who wishes to embark on the journey to learn MongoDB, the most popular NoSQL database on this day, according to www.db-engines.com. Learning a popular technology also ensures that you cement your relevance in the job market, isn't it !!

In order to safeguard the learner from the information deluge on the subject, which might leave him perplexed,  this guide will take a very structured approach in disseminating  the information.

Let us kickstart,

What do we need to setup up a working MongoDB environment,

  1. Identify the platform that you are on. I personally prefer to work on Ubuntu. Not for the reason that it's free, but most of the production installations are on one or the other Unix flavours that match very closely to Ubuntu, besides, the Long Term Support (LTS) that you get on Ubuntu is quite inviting. I use Ubuntu 14.04. To the undergrads who are reading this primer, I would highly recommend using a Linux flavour of their choice for the reasons that they would really appreciate when they would be into their careers.
  2. Download a copy of the latest MongoDB for your platform. It is always good to learn on the latest GA releases, the current one is 3.2.1.
  3. Starting the Server,
    1. Open the Terminal window (Ctrl + Alt + T). Unzip and untar the downloaded archive - $ tar -zxvf mongodb-linux-x86_64-ubuntu1404-3.2.1.tgz
    2. Rename the folder to a shorter name and move it to your home directory - $ mv mongodb-linux-x86_64-ubuntu1404-3.2.1 ~/MongoDB_3.2.1
    3. Create directories for storing Mongo work files -  $ mkdir ~/MongoDbWorkSpace ~/MongoDbWorkSpace/Data ~/MongoDbWorkSpace/Logs - This creates directories for storing MongoDB data files and logs.
    4. Reach out to the bin directory in the install base - $ cd ~/MongoDB_3.2.1/bin - Takes you to the bin directory.
    5. Start the server $ ./mongod --dbpath ~/MongoDbWorkSpace/Data --storageEngine wiredTiger --logpath ~/MongoDbWorkSpace/Logs/mongod.log --fork - mongod is the server process; here I am using the new storage engine introduced in version 3.0 "wiredTiger", which offers greater capabilities as compared to the earlier and still available, mmapv1 (more on this in later series); the server log is generated in mongod.log and this will come to your rescue in case something goes wrong and you need to debug it; I am using the fork option to enable me to continue using the same terminal window as the control comes back to the shell and the process runs in the background.
    6. Once the server successfully starts, it will return a process id. You can further drill down into the Data and Log directories and look at the newly created directories and files.
  4. Connecting to the Server,
    1. Again reach out to the bin directory in the install base - $ cd ~/MongoDB_3.2.1/bin - Takes you to the bin directory.
    2. Connect using the shell - $ mongo - voila, you are now connected to the MongoDB Server. The Server is available on the default port number, 27017. You can also specify  a different port number than the default by using --port <number> option when starting the server i.e. the mongd process as in step 3(e).
  5. Working the Mongo Shell,
    1. You are now good to run any of the shell commands
      1. > show dbs - Lists the available databases.
      2. > use test - Attaches you to test database. You can attach to a non-existent database and once you create a collection then the database  gets created as well if it did not exist earlier. 
      3. > db - Lists the database that you are attached to.
      4. db.myFirstCollection.insert({a:"Hello World"}) - Inserts a document in the collection myFirstCollection. In case the collection didn't exist, it creates the collection as well. Documents are analogous to rows in tables in the RDBMS world.
      5. > show collections - Lists the collections in the database that you are attached to. Collections are analogous to tables in the RDBMS world.
      6. > db.myFirstCollection.find() - Lists all the documents in the collection.
      7. > db.help() - Lists the available helper methods on the db object.
      8. > db.shutdownServer() - To shutdown the server, but attach to the admin db first by running 'use admin'.
      9. > exit - To exit the mongo shell.

MongoDB is a document data store. These documents are represented as JSON.

In this primer you have been initiated on how to configure and start using MongoDB. In the upcoming Step 2 of this series, I will discuss more on "MongoDB CRUD and Administrative Commands" and alternate ways of connecting to the Server. Till then, tinker with the newly setup MongoDB environment.

 

Recommended for further reading,

  1. Read more about JSON at, www.json.org
  2. MongoDB shell help at, docs.mongodb.org

 

EmailWhatsAppTwitterLinkedInGoogle+FacebookShare

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>