MongoDB Introduction and Setup


MongoDB is a NoSQL, document based database. It is free and open-source software. In this article, we will come to know the basic units of MongoDB and its setup.

MongoDB Introduction

We all know a relation database consists of mostly tables and views. Mongo database on other hand, consists of collections, a collection is analogous to a relational database’s table. Each collection contains a set of MongoDB documents. You can think of document as a table’s row. Each document in turn is composed of key-value pairs, similar to JSON objects.

Each document must have a unique _id field that acts as a primary key.

MongoDB Setup

MongoDB runs on various platforms and supports both 32-bit and 64-bit architectures. You need to first download the correct version of MongoDB based on your platform.

  1. Here we will be setting up MongoDB on windows 7, 64-bit version.
    Download MongoDB.
  2. Next, run the downloaded MSI installer file. In case you want to chose specific download location other than the default one, you may chose the ‘Custom’ way, provide directory path where you want the downloaded files. We have installed mongoDB in c:\mongoDB.
  3. Now we need to create a data directory where the database files of MongoDB will reside. Create a data directory data within the the mongoDB directory.
  4. To start MongoDB, run
    C:\mongodb\bin>mongod.exe --dbpath "C:\mongodb\data"
  5. Once started, you should see ‘Waiting for connections on port’ as the last line in console.
    C:\mongodb\bin>mongod.exe --dbpath "C:\mongodb\data"
    2015-12-31T12:52:14.324+0530 I CONTROL  [main]Hotfix KB2731284 or later update
    is not installed, will zero-out data files
    2015-12-31T12:52:14.328+0530 I CONTROL  [initandlisten]MongoDB starting : pid=1
    4468 port=27017 dbpath=C:\mongodb\data 64-bit host=INMAA1-L1005
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]targetMinOS: Windows 7/W
    indows Server 2008 R2
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]db version v3.2.0
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]git version: 45d947729a0
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]OpenSSL version: OpenSSL
     1.0.1p-fips 9 Jul 2015
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]allocator: tcmalloc
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]modules: none
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]build environment:
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]distmod: 2008plus-ss
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]distarch: x86_64
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]target_arch: x86_64
    2015-12-31T12:52:14.329+0530 I CONTROL  [initandlisten]options: { storage: { db
    Path: "C:\mongodb\data" } }
    2015-12-31T12:52:14.378+0530 I -        [initandlisten]Detected data files in C
    :\mongodb\data created by the 'wiredTiger' storage engine, so setting the active
     storage engine to 'wiredTiger'.
    2015-12-31T12:52:14.379+0530 I STORAGE  [initandlisten]wiredtiger_open config:
    2015-12-31T12:52:15.295+0530 I NETWORK  [HostnameCanonicalizationWorker] Startin
    g hostname canonicalization worker
    2015-12-31T12:52:15.295+0530 I FTDC     [initandlisten]Initializing full-time d
    iagnostic data capture with directory 'C:/mongodb/data/'
    2015-12-31T12:52:15.300+0530 I NETWORK  [initandlisten]waiting for connections
    on port 27017

MongoDB Shell

Now that we have installed MongoDB let’s interact with it using ‘Mongo Shell’ command line program.

2015-12-31T13:00:25.617+0530 I CONTROL  [main]Hotfix KB2731284 or later update
is not installed, will zero-out data files
MongoDB shell version: 3.2.0
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
Questions? Try the support group

Insert Document using MongoDB Shell

Let’s insert a sample document using MongoDB shell. We will create documents in database called mydb.
In order to do so, first we need to switch the database. By default when you run ‘mongo’ shell, MongoDB will use ‘test’ database.

> use mydb
switched to db mydb

Databases and collections are first created when documents are actually inserted so even though we have switched over the database to use mydb, mydb is not yet created.

< show dbs
admin                                      0.000GB
complexIdTest                              0.000GB
database                                   0.001GB
issue                                      0.000GB
local                                      0.000GB
mongo-index-db1                            0.000GB
mongoRepositoryTextSearchIntegrationTests  0.000GB
repositories                               0.000GB
script-tests                               0.000GB
test                                       0.000GB
validation                                 0.000GB

You can insert a document using db.{collection name}.insert. Below statement inserts some product details, collection name is product.

< db.product.insert({"product": "Comfi Storage Bed", "finish": "teak", "size": "king", "price": 42999, "desc": "Bring home the style"})

After insert, the write result shows up:

WriteResult({ "nInserted" : 1 })

You can fetch the inserted documents using db.{collection name}.find().

{ "_id" : ObjectId("5684ef074e82c53109540b7c"), "product" : "Comfi Storage Bed",
 "finish" : "teak", "size" : "king", "price" : 42999, "desc" : "Bring home the style" }

You would have noticed that the _id field has been added to the document. This acts as a primary key and since we haven’t provided one, mongoDB generated it.


Comments are closed.