Skip to main content

MongoDB Setup & Overview



MongoDB is an open source, document oriented NoSQL database where data is represented & stored in the form of BSON(binary JSON) ” 

Following is a key value representation of data in JSON format and its corresponding BSON.It adds some "extra" information to documents, like length of string and sub-objects. This makes traversal faster.

// JSON file
   { "hello" : "world" }

// BSON file
"\x16\x00\x00\x00\x02hello\x00
\x06\x00\x00\x00world\x00\x00"


 
  \x16\x00\x00\x00                   // total document size
  \x02                               // 0x02 = type String
  hello\x00                          // field name
  \x06\x00\x00\x00world\x00          // field value
  \x00                               // 0x00 = type end of object 
 
With the growing use of Social Media platforms, large chunks of unstructured data was created .Thus, a need to to persist this unstructured data in the most efficient way possible hence NoSQL databases came into existence.
Following is a brief comparison between them,


SQL
No-SQL
  • Relational
  • Table
  • Predefined Schema
  • Vertically scalable
  • Better for transactions
  • Hierarchical
  • Document
  • Dynamic or No Schema
  • Horizontally scalable
  • Faster I/O

When between faster I/O and disk-space the choice is faster computation time, irrespective of the fact that we are duplicating the data then we must go for MongoDB


RDBMS
Mongo
  • Table,View
  • Row
  • Index
  • Join
  • Foreign Key
  • Partition
  • Collection
  • Document
  • Index
  • Embedded
  • Reference
  • Shard

Installation on Ubuntu

Inorder to install mongoDB on ubuntu, make sure you have a stable version(here 16.4) of Ubuntu & root privileges to the current ubuntu user & Follow the steps
  1. On the terminal screen, run following command to import MongoDB keys to your server.
    mCreate a MongoDB list file in /etc/apt/sources.list.d/ with this command,
  2. echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-o
    rg/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
  1. Update the repository with apt command
    sudo apt-get update
  2. Install MongoDB
    sudo apt-get install -y mongodb-org
  1. Create file ‘mongodb.service’
    sudo nano /lib/systemd/system/mongodb.service
and copy the file contents as follows
[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target

[Service]
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongodb
Group=mongodb
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target
Save the file and exit.

  1. Now update the systemd service with command below,
    systemctl daemon-reload
       create db folder at /data
cd /
sudo mkdir data
cd /data
sudo mkdir db

To start mongoDB service
sudo service mongod start
 
      Start service at boot time
      systemctl start mongod
      systemctl enable mongod

  1. Start MongoDB without access control. 
    mongod --port 27017 --dbpath /data/db
        on another terminal, Connect to the instance.
        mongo --port 27017

Basic Operations
  • To create database
> use testdb
switched to db testdb

  • To list all databases
> show dbs
testdb 0.000GB

  • To create a collection
> db.createCollection(“company”)
{ "ok" : 1 }

  • To list all collections
> show collections
company
  • To insert a document
> db.company.insert({empName:'aaa',empNo:101})
WriteResult({ "nInserted" : 1 })

  • To display the document
> db.company.find();
{ "_id" : ObjectId("5923e409848dfe0c013c5977"), "empName" : "aaa", "empNo" : 101 }
> db.company.find().pretty();
{
"_id" : ObjectId("5923e409848dfe0c013c5977"),
"empName" : "aaa",
"empNo" : 101
}
>


Comments

Popular posts from this blog

Redis Basic CRUD

We have seen how to setup on your linux machine here , now we will see how to perform basic CRUD operations using Spring Data & Redis server We will be creating a simple application that would persist an employee information into redis database. You will be needing certain JARs like jedis.jar, spring-data-redis.jar etc details of which you can download and view at https://github.com/meta-magic/RedisCRUDexample.git  First of all we will be creating the Employee entity, plz note that we are using the Serializable interface which automatically mapps the objects against the key. Employee.java import java.io.Serializable ; public class Employee implements Serializable { private static final long serialVersionUID = - 8243145429438016231L ; public enum Gender { Male , Female } private String id; private String name; private Gender gender; private int age; public String getId () { return id; } public void setId ( String

Function Point Analysis : ISO/IEC 20926:2009

This blog focuses on explaining the Function Point calculations. Software Sizing Background Function Point Rules for Counting FP Deep Dive - Function Point Analysis Case Study General Software Characteristics Details History - Measurement Methodologies Lines of Code (Oldest) Use case based Software Sizing IPFUG Function Point Analysis (ISO) Need for Software Sizing. Estimation and Budgeting Phasing Development Work Prioritization of Work Monitoring the Progress Bidding for Projects Allocating Testing Resources To measure and Manage Productivity Risk Assessment Software Asset Valuation CMMi Level 2 and 3 require that a valid sizing method be used. Software Sizing - Lines of Code The easiest and historically the most common method in Sizing Software project has been counting the number of lines of code and / or the number of screens. Advantages Automation of the counting process can be done Intuitive as the measurements are easily u

Redis Installation Steps

“ Redis is an in-memory key-value store known for its flexibility, performance, and wide language support” Inorder to install redis on your machine you need ubuntu 16.4 and a non-root user with sudo privileges to perform the administrative functions required for this process. Download and Extract the Source Code Create a tmp directory cd /tmp Download the latest stable version of Redis curl -O http://download.redis.io/redis-stable.tar.gz untar tar xzvf redis-stable.tar.gz Move into the Redis source directory structure that was just extracted cd redis-stable Build and Install Redis Now, we can compile the Redis binaries by typing make After the binaries are compiled, run the test suite to make sure everything was built correctly. You can do this by typing: make test This will typically take a few minutes to run. Once it is complete, you can install the binaries onto the system by typing: sudo