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

Eureka-Server with spring cloud netflix

In this write-up we will focus on Service Registry – Eureka Server Rest service (auth-service application, eureka client) which register itself to registry. Web application which consumes Rest service using service registry. Service discovery  allows services to find and communicate with each other without hardcoding hostname and port. Eureka Server In spring-boot application enable the Eureka-Server by adding @EnableEurekaServer annotation to spring boot application. We have put all the configuration on GIT and this is accessed using config-server. To now more about centralized configuration (config-server) click  here eurekaserver.yml Place below bootstrap.yml in application, it basically connects to config-server and gets it required configuration. Start the spring-boot application and access eureka server using http://localhost:8760/ you will get below screen. Right now there is no application which is registered to e

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