MongoDB for Developers – Getting Started

Introduction

As part of this getting started or 101 course we will see how Java developers can start learning MongoDB. Here is the course link.

Course topics

Following are the topics covered:

  • Setup MongoDB
  • MongoDB data architecture
  • Mongo Shell
  • CRUD operations
  • Mongo chef
  • Data Model
  • Setup development environment
  • Develop simple java application
  • Perform CRUD operations using java

Setup MongoDB

MongoDB is very lightweight and straight forward to install and then start the database locally for development purposes.

Here are the steps involved in setup process:

  • Download the database
  • Install the database
  • Start the database

Setup instructions in Mac

Here is the video for setup process

  • Go to MongoDB Download Center
  • Download OSX version (a tgz file will be downloaded)
  • Unpackage by double clicking on tgz file (if there is no unzip tool, install using tar xzf command)
  • Copy the directory to home directory and create soft link
cp -rf ~/Downloads/mongodb-osx-x86_64-3.2.7 ~
ln -s ~/mongodb-osx-x86_64-3.2.7 ~/mongodb
  • Create soft link ln -s
  • Add environment variables to ~/.profile
export MONGO_HOME=/Users/dgadiraju/mongodb
export PATH=$PATH:$MONGO_HOME/bin
  • Create directories
mkdir -p ~/mongodb/logs
mkdir -p ~/mongodb/data/db
  • Start mongo server using command

mongod --dbpath /Users/dgadiraju/mongodb/data/db --fork --logpath /Users/dgadiraju/mongodb/logs/mongod.log

  • Validate mongo installation by running mongo
  • Sample commands
show dbs;
use local;
show collections;

Data Architecture of MongoDB

Developers need to understand that

  • MongoDB stores data in the form collections and documents (in RDBMS, it is tables and rows/records)
  • MongoDB collections are schema less, each documents will have different number and different types of elements.
  • MongoDB is NoSQL database
  • Supports CRUD – Create, Read, Update and Delete
  • No transactions (eg: commit or rollback), each operation is automatically committed
  • Rich querying capabilities (including aggregation framework)

Overview of Mongo Shell

Mongo shell is command line interface to interact with MongoDB server and perform actions such as

  • Managing MongoDB databases
  • Create collections
  • Validate queries
  • many more

Here is the video which covers introduction part of Mongo Shell

  • Make sure Mongo server daemon “mongod” is running
  • Launch Mongo Shell (using mongo command)
  • Following code snippet will create database called hr and collection called employees and insert a record into it.
  use hr;
  db.employees.insert({"empno" : 1, "ename" : "Some Person"})

Overview of CRUD

CRUD are the basic operations in a database. MongoDB supports all CRUD operations.

  • Create – stands for inserting elements into collections
  • Read – stands for querying elements from collections
  • Update – Updating elements in collections
  • Delete – Removing elements from collections

Here is the video which covers Create and Read (Insert and Query)

Inserting data into Mongo collection

  • db.<collection_name>.insert is the command to insert the data
  • Each document will be in the form of { “col1_name” : “col1_value”, “col2_name”:”col2_value”}
  • Basic insert command
db.employees.insert({
    "empno" : 1,
    "ename" : "some person"
}
  • Documents can be nested – which means there can be document with in a document
  • Inserting nested document – dept is nested document
db.employees.insert({
    "empno" : 2,
    "ename" : "some other person",
    "salary" : 1000.5,
    "dept" : {
        "dname" : "IT",
        "location" : "Dallas",
        "dmgr" : "some person"
    },
    "commpercent" : 10
}
  • Results can be validated by running db.employees.find().pretty()
  • Output
{
    "_id" : ObjectId("577f4b478770505311518251"),
    "empno" : 1,
    "ename" : "some person"
}
{
    "_id" : ObjectId("577f9109d2f21ce7c717dea7"),
    "empno" : 2,
    "ename" : "some other person",
    "salary" : 1000.5,
    "dept" : {
        "dname" : "IT",
        "location" : "Dallas",
        "dmgr" : "some person"
    },
    "commpercent" : 10
}
  • _id is system generated unique id, we can insert other unique values as part of _id. Default datatype for _id is ObjectId but we can store other types as well. But it has to be unique.
  • Both records returns element/column names and values
  • Number of elements/columns are different between both the documents in the collection.

Querying data from Mongo Collection (basic operations)

  • We have already seen basic usage of find as part of validation of insert
  • We can apply many filtering conditions as part of find command, such as comparison operators, boolean operators etc
  • Here is the command to apply basic filter conditions
  • Below code snippet for select * from employees where empno = 1

db.employees.find({"empno" : 1})

  • Below code snippet for select * from employees where salary > 1000

db.employees.find({"salary" : {"$gt" : 1000 }}).pretty()

  • Below code snippet for select _id, empno, salary from employees where salary > 1000
db.employees.find(
{"salary" : {"$gt" : 1000 }}, /* select - where salary > 1000 */
{"empno" : 1, "salary" : 1} /* projection - select _id, empno, salary */
  /* _id which is key to the collection will be displayed unless it is added in projection */
  /* like {"empno" : 1, "salary" : 1, "_id" : 0}*/
)
  • pretty() can be applied on find to prettify the output of the command
  • We can perform many advanced operations

Here is the video which covers Update and Delete

 

Add Comment

Leave a Reply

shares

Big Data Introduction - YouTube live video

Please click here

Subscribe!