Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

ABSTRACT

MongoDB is an open-source
document database and leading
NoSQL database. This tutorial will give
you great understanding on MongoDB
concepts and CRUD operations used
for creating unstructured database.

Lab Exercise
(based on NoSQL
MongoDB)
Contents
I. Introduction.............................................................................................................................2
Advantages of MongoDB over RDBMS.............................................................................3
Why Use MongoDB?.........................................................................................................4
Where to Use MongoDB?..................................................................................................4
II. Create a database..................................................................................................................5
III. The dropDatabase() Method..................................................................................................6
IV. The createCollection() Method...............................................................................................7
V. Drop a Collection using the drop() Method............................................................................8
VI. Insert documents in the following collections.........................................................................9
VII. Displaying Information..........................................................................................................18
VIII. Appendix – A........................................................................................................................34
IX. Appendix – B........................................................................................................................35
X. Appendix – C........................................................................................................................36

WiDS Workshop- Introduction to NoSQL MongoDB Page 1 of 41


I. Introduction
MongoDB is an open-source document database and leading NoSQL database. MongoDB is
written in C++. This tutorial is designed for Software Professionals who are willing to learn
MongoDB Database in simple and easy steps.
MongoDB is a cross-platform, document-oriented database that provides, high performance,
high availability, and easy scalability. MongoDB works on concept of collection and document.

Database
Database is a physical container for collections. Each database gets its own set of files
on the file system. A single MongoDB server typically has multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table.
A collection exists within a single database. Collections do not enforce a schema.
Documents within a collection can have different fields. Typically, all documents in a
collection are of similar or related purpose.
Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic
schema means that documents in the same collection do not need to have the same
set of fields or structure, and common fields in a collection's documents may hold
different types of data.
The following table shows the relationship of RDBMS terminology with MongoDB.

RDBMS MongoDB

Database Database

Table Collection

Tuple/Row Document

column Field

Table Join Embedded Documents

Primary Key Primary Key (Default key _id provided by


MongoDB itself)

WiDS Workshop- Introduction to NoSQL MongoDB Page 2 of 41


Sample Document
Following example shows the document structure of a blog site, which is simply a
comma separated key value pair.
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},
{
user:'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}

_id is a 12 bytes hexadecimal number which assures the uniqueness of every


document. You can provide _id while inserting the document. If you don’t provide then
MongoDB provides a unique id for every document. These 12 bytes first 4 bytes for the
current timestamp, next 3 bytes for machine id, next 2 bytes for process id of MongoDB
server and remaining 3 bytes are simple incremental VALUE.

Advantages of MongoDB over RDBMS


 Schema less − MongoDB is a document database in which one collection holds
different documents. Number of fields, content and size of the document can
differ from one document to another.
 Structure of a single object is clear.
 No complex joins.
 Deep query-ability. MongoDB supports dynamic queries on documents using a
document-based query language that's nearly as powerful as SQL.
 Tuning.
 Ease of scale-out − MongoDB is easy to scale.
 Conversion/mapping of application objects to database objects not needed.

WiDS Workshop- Introduction to NoSQL MongoDB Page 3 of 41


 Uses internal memory for storing the (windowed) working set, enabling faster
access of data.

Why Use MongoDB?


 Document Oriented Storage − Data is stored in the form of JSON style
documents.
 Index on any attribute
 Replication and high availability
 Auto-Sharding
 Rich queries
 Fast in-place updates
 Professional support by MongoDB

Where to Use MongoDB?


 Big Data
 Content Management and Delivery
 Mobile and Social Infrastructure
 User Data Management
 Data Hub

WiDS Workshop- Introduction to NoSQL MongoDB Page 4 of 41


II. Create a database
Syntax: use DATABASE_NAME

Create a database named “company”


use company
switched to db company

NOTE: If the database company does not exist, it will create it


To check your currently selected database, use the command db
show dbs
local 0.78125GB
test 0.23012GBIn the Company Database create 4
collections
Your created database (company) is not present in list. To display database, you need
to insert at least one document into it.
In MongoDB default database is test. If you didn't create any database, then collections
will be stored in test database.

WiDS Workshop- Introduction to NoSQL MongoDB Page 5 of 41


III.The dropDatabase() Method
MongoDB db.dropDatabase() command is used to drop a existing
database.
Syntax
Basic syntax of dropDatabase() command is as follows −
db.dropDatabase()
This will delete the selected database. If you have not selected any
database, then it will delete default 'test' database.
Example
First, check the list of available databases by using the command, show
dbs.
>show dbs
local 0.78125GB
company 0.23012GB
test 0.23012GB
>
If you want to delete new database <company>,
then dropDatabase() command would be as follows −
>use company
switched to db company
>db.dropDatabase()
>{ "dropped" : "company", "ok" : 1 }
>
Now check list of databases.
>show dbs
local 0.78125GB
test 0.23012GB
>

WiDS Workshop- Introduction to NoSQL MongoDB Page 6 of 41


IV. The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.

Syntax
Basic syntax of createCollection() command is as follows −
db.createCollection(name)
In the command, name is name of collection to be created. Options is a document and
is used to specify configuration of collection.

Examples
Basic syntax of createCollection() method without options is as follows −

use test
switched to db test
db.createCollection("mycollection")

You can check the created collection by using the command show collections.

show collections
mycollection
system.indexes

In MongoDB, you don't need to create collection. MongoDB creates


collection automatically when you insert some document.

WiDS Workshop- Introduction to NoSQL MongoDB Page 7 of 41


V. Drop a Collection using the drop()
Method
MongoDB's db.collection.drop() is used to drop a collection from the database.

Syntax
Basic syntax of drop() command is as follows −
db.COLLECTION_NAME.drop()
Example
db.mycollection.drop()
drop() method will return true, if the selected collection is dropped successfully,
otherwise it will return false.
Data Types used iin MongoDB
MongoDB supports many datatypes. Some of them are −
 String − This is the most commonly used datatype to store the data. String in
MongoDB must be UTF-8 valid.
 Integer − This type is used to store a numerical value. Integer can be 32 bit or
64 bit depending upon your server.
 Boolean − This type is used to store a boolean (true/ false) value.
 Double − This type is used to store floating point values.
 Arrays − This type is used to store arrays or list or multiple values into one key.
 Timestamp − timestamp. This can be handy for recording when a document has
been modified or added.
 Object − This datatype is used for embedded documents.
 Null − This type is used to store a Null value.
 Date − This datatype is used to store the current date or time in UNIX time
format. You can specify your own date time by creating object of Date and
passing day, month, year into it.
 Object ID − This datatype is used to store the document’s ID.
 Binary data − This datatype is used to store binary data.

WiDS Workshop- Introduction to NoSQL MongoDB Page 8 of 41


VI. Insert documents in the following
collections
1. Project
2. department
3. employee

DepName

Relation DepSec

‘Ann’
‘Daughter’ ‘F’

Insert one record in the project collection


db.project.insertOne(

WiDS Workshop- Introduction to NoSQL MongoDB Page 9 of 41


{_id:"2", PName:"ProductY", Plocation:"Sugarland",
ConDep:"4", worker:[{WorkerId:"987654321",Hours:10},
{WorkerId:"333445555",Hours:6}]
})
db.project.insertOne(
{
_id:"3", PName:"ProductZ", Plocation:"Houston",
"ConDep":"4", worker:[{WorkerId:"888665555",Hours:50},
{WorkerId:"987654321",Hours:6}]
}
)

Insert many records in the project collection


db.project.insertMany([
{_id:"10", PName:"Computerization",
Plocation:"Stafford", ConDep:"1", worker:
[{WorkerId:"888665555",Hours:8},
{WorkerId:"333445555",Hours:20}]},
{_id:"20", PName:"Reorganization", Plocation:"Houston",
ConDep:"5", worker:[{WorkerId:"987654321",Hours:6},
{WorkerId:"999887777",Hours:16}]},
{_id:"30", PName:"Newbenefits",
Plocation:"Stafford","ConDep":"5", worker:
[{WorkerId:"444444403",Hours:20},
{WorkerId:"555555501",Hours:30}]},
{_id:"61", PName:"OperatingSystems",
Plocation:"Jacksonville","ConDep":"1", worker:
[{WorkerId:"987654321",Hours:40},
{WorkerId:"333445555",Hours:15}]},
{_id:"62", PName:"DatabaseSystems",
Plocation:"Birmingham","ConDep":"4", worker:

WiDS Workshop- Introduction to NoSQL MongoDB Page 10 of 41


[{WorkerId:"888665555",Hours:50},
{WorkerId:"666884444",Hours:5}]},
{_id:"63", PName:"Middleware",
Plocation:"Jackson","ConDep":"5", worker:
[{WorkerId:"888665555",Hours:40},
{WorkerId:"333445555",Hours:16}]},
{_id:"91", PName:"InkjetPrinters",
Plocation:"Phoenix","ConDep":"8", worker:
[{WorkerId:"555555501",Hours:19},
{WorkerId:"444444401",Hours:26}]},
{_id:"92", PName:"LaserPrinters",
Plocation:"LasVegas","ConDep":"7", worker:
[{WorkerId:"987654321",Hours:30},
{WorkerId:"111111101",Hours:1}]}
])
Insert one record in the department collection
db.department.insertOne(
{_id:"4",Dname:"Administration",MgrSSn:"987654321",MgrS
tDate:"20-JUN-2019",DLocations:["Stafford", "Seattle"]
})
Insert many records in the department collection
db.department.insertMany([
{_id:"1",Dname:"Headquaters",MgrSSn:"888665555",
MgrStDate:"08-DEC-2017", DLocations:["Houston",
"Seattle"]},
{_id:"5",Dname:"Research",MgrSSn:" 333445555",
MgrStDate:"09-JAN-2016", DLocations:
["Houston","Chicago", "Seattle"]},
{_id:"6",Dname:"Software",MgrSSn:"111111101",
MgrStDate:"08-DEC-2020", DLocations:["Sacramento",
"Atlanta"]},

WiDS Workshop- Introduction to NoSQL MongoDB Page 11 of 41


{_id:"7",Dname:"Hardware",MgrSSn:"444444401",
MgrStDate:"08-DEC-2019", DLocations:["Milwaukee"]},
{_id:"8",Dname:"Sales",MgrSSn:"555555501",
MgrStDate:"08-DEC-2018", DLocations:["Chicago",
"Dallas","Philadephia","Miami","Seattle"]}
])
Insert one record in the employee collection
db.employee.insertOne(
{
_id:"888665555",Fname:"James", Minit:"E", LName:"Borg",
dBirth:"10-Nov-2007",Address: "450 Stone Houston TX",
Gender: "M", Salary:55000,
Dnumber:"5", Dependents:[{DepName:"Ann", DepGen:"F",
Relation:"Daughter"}]
})
Insert many records in the employee collection
db.employee.insertMany([
{
_id:"333445555",Fname:"Franklin", Minit:"T",
LName:"Wong",
dBirth:"08-DEC-2015",Address: "638 Voss Houston TX",
Gender: "M",
Salary:40000, Dnumber:"4",
Dependents:
[
{DepName:"Alice", DepGen:"F", Relation:"Daughter"},
{DepName:"Theodre", DepGen:"M", Relation:"Son"},
{DepName:"Joy", DepGen:"M", Relation:"Spouse"}]

WiDS Workshop- Introduction to NoSQL MongoDB Page 12 of 41


},
{
_id:"987654321",Fname:"Jennifer", Minit:"B",
LName:"Wallace",
dBirth:"09-Jan-2005",Address: "731 Fondren Houston TX",
Gender: "M", Salary:30000,
Dnumber:"5",
Dependents:
[{DepName:"Abner", DepGen:"F", Relation:"Spouse"}]
},

{_id:"999887777",Fname:"Alicia", Minit:"J",
LName:"Zelaya",
dBirth:"19-JUL-1995",Address: "3321 Castle, Spring,
TX", Gender: "F", Salary:25000,
Dnumber:"4",
Dependents:
[
{DepName:"Mary", DepGen:"F", Relation:"Daughter"},
{DepName:"Sally", DepGen:"F", Relation:"Daughter"}]
},

{_id:"666884444",Fname:"Ramesh", Minit:"K",
LName:"Narayan",
dBirth:"15-SEP-2010",Address: "971 Fire Oak, Humble,
TX", Gender: "M", Salary:43000,
Dnumber:"4",
Dependents:

WiDS Workshop- Introduction to NoSQL MongoDB Page 13 of 41


[
{DepName:"Mathew", DepGen:"M", Relation:"Son"}]
},

{_id:"453453453",Fname:"Joyce", Minit:"A",
LName:"English",
dBirth:"31-JUL-1990",Address: "5631 Rice Oak, Houston,
TX", Gender: "F", Salary:25000,
Dnumber:"5",
Dependents:[
{DepName:"Issa", DepGen:"M", Relation:"Son"},
{DepName:"Mousa", DepGen:"M", Relation:"Spouse"}]
},

{_id:"987987987",Fname:"Ahmad", Minit:"V",
LName:"Jabbar",
dBirth:"29-MAR-1990",Address: "980 Dallas, Houston,
TX", Gender: "M", Salary:25000,
Dnumber:"4",
Dependents:
[
{DepName:"Azhar", DepGen:"M", Relation:"Son"},
{DepName:"Maryam", DepGen:"F", Relation:"Spouse"}]
},

{_id:"111111101",Fname:"Jon", Minit:"C", LName:"Jones",

WiDS Workshop- Introduction to NoSQL MongoDB Page 14 of 41


dBirth:"20-JUN-2013",Address: "111 Allgood, Atlanta,
GA", Gender: "M", Salary:45000,
Dnumber:"6",
Dependents:
[
{DepName:"Jemica", DepGen:"F", Relation:"Daughter"},
{DepName:"Mona", DepGen:"F", Relation:"Spouse"}]
},

{_id:"555555501",Fname:"Nandita", Minit:"K",
LName:"Ball",
dBirth:"20-JUN-2013",Address: "222 Howard, Sacramento,
CA", Gender: "F", Salary:62000,
Dnumber:"8",
Dependents:
[
{DepName:"Sonam", DepGen:"F", Relation:"Daughter"}]
},

{_id:"444444401",Fname:"Bonnie", Minit:"S",
LName:"Bays",
dBirth:"19-JUN-1985",Address: "111 Hollow, Milwaukee,
WI", Gender: "F", Salary:70000,
Dnumber:"7",
Dependents:
[
{DepName:"Rennie", DepGen:"M", Relation:"Spouse"},

WiDS Workshop- Introduction to NoSQL MongoDB Page 15 of 41


{DepName:"John", DepGen:"M", Relation:"Son"}]
},

{_id:"666666601",Fname:"Jill", Minit:"J",
LName:"Jarvis",
dBirth:"17-APR-1978",Address: "8794 Garfield, Chicago,
IL", Gender: "M", Salary:96000,
Dnumber:"8",
Dependents:
[{DepName:"Franky", DepGen:"F", Relation:"Spouse"}]
},

{_id:"444444403",Fname:"Sam", Minit:"S",
LName:"Snedden",
dBirth:"31-JUL-1985",Address: "987 Windy St, Milwaukee,
WI", Gender: "M", Salary:48000,
Dnumber:"7",
Dependents:
[
{DepName:"Sam", DepGen:"M", Relation:"Son"}]
}
])
How to insert record in a date format.
db.employee.insert([
{
"_id" : "966884444",
"Fname" : "Rahul",

WiDS Workshop- Introduction to NoSQL MongoDB Page 16 of 41


"Minit" : "K",
"LName" : "Kumar",
"dBirth" : new Date(2019, 03, 29, 13, 12),
"Address" : "971 Fire Oak, Humble, TX",
"Sex" : "M",
"Salary" : 93000,
"Dnumber" : "4",
"Dependents" : [
{
"DepName" : "Amol",
"DepSec" : "M",
"Relation" : "Son"
}
]
}
])

WiDS Workshop- Introduction to NoSQL MongoDB Page 17 of 41


VII. Displaying Information
To display ALL documents in a collection : Find()
find() method will display all the documents in a non-structured way.
Syntax:
db.collectionName.find({})
OR
db.collectionName.find()
Example:
db.department.find()
Display documents of employee.
db.employee.find()
To display documents in a formatted way, you can use pretty()
method: Find().pretty():
Syntax:
db.Collection_Name.find().pretty()
Display documents of employee in an organized manner
db.employee.find().pretty()
To display documents in a Sorted manner : Sort()
The method accepts a document containing a list of fields along with their
sorting order.
To specify sorting order 1 and -1 are used. 1 is used for ascending order
while -1 is used for descending order.
Syntax
>db.Collection_Name.find().sort({KEY:1})
Display documents in the ascending order of key value
db.employee.find().pretty().sort({"_id":1})

WiDS Workshop- Introduction to NoSQL MongoDB Page 18 of 41


Display documents in the descending order
db.locations.find().pretty().sort({"_id":-1})
How to display documents in the ascending order of gender
db.employee.find().pretty().sort({"Gender":1})
How to display documents in the descending order of gender
db.employee.find().pretty().sort({"Gender":-1})
Display employee records in descending order of first name. If the
first names are the same, then sort them according to the ascending
order of the lastname.
db.employee.find().pretty().sort({"Fname":-1,"LName":1})
To count the number of the documents obtained from a query: count()
Display the total number of documents in department collection.
db.department.find().count()
OR
db.department.count()
To display documents based on some conditions
Syntax:
db.collection_name.find({criteria1,{criteria 2},…)
Display all attributes of an employee whose first name is Jon
db.employee.find({Fname: "Jon"}).pretty()
Display the employee whose Ssn is 987987987
db.employee.find({_id: "987987987"}).pretty()
Display the female employees(s).
db.employee.find({"Sex":"F"}).pretty()
Display the departments which have location is in Seattle.
NOTE: Dlocations has multi-values which are stored in an array
db.department.find({DLocations:"Seattle"}).pretty()

WiDS Workshop- Introduction to NoSQL MongoDB Page 19 of 41


Display the employees who have female dependents.
NOTE: Dependent is an object which includes field DepName,
DepGen, Relation
db.employee.find({"Dependents.DepGen":"F" }).pretty()
Display the female employees having atleast one female dependents.
db.employee.find({Gender:"F","Dependents.DepGen":"F"}).
pretty()
Display the total number of female employees
db.employee.find({"Sex":"F"}).count()
Display the employee documents having salary greater than 40000
Operators
db.employee.find({"Salary": {$gt:40000}}).pretty()
Display the employees whose salary is less than 50000.
db.employee.find({Salary: {$lt:50000}}).pretty()
Display the employees having salary between 45000 to 70000
db.employee.find({$and:[{Salary:{$gte:45000}},{Salary:
{$lte:70000}}]}).pretty()
Display the FEMALE employees whose salary is greater than 60000.
db.employee.find({ $and:[{Salary:{$gt:60000}},
{Gender:"F"}]}).pretty()
OR
db.employee.find({ $and:[{"Salary":{$gt: 60000}},
{Gender:{$eq:"F"}}]}).pretty()
Display the employees who work in department number 4 or 5.
db.employee.find({Dnumber: { $in: [ "4",
"5" ] } }).pretty()
Display the employees whose date of birth is greater than 01-01-2001

WiDS Workshop- Introduction to NoSQL MongoDB Page 20 of 41


db.employee.find({dBirth: {$gt: new Date('2000-01-01') }})
Display the departments which have location either in Seattle or in
Houston.
NOTE: Dlocations has multi-values which are stored in an array
db.department.find({DLocations:{$in:
["Seattle","Houston"]}}).pretty()
OR
db.department.find({$or:[{DLocations:"Houston"},
{DLocations:"Seattle"}]}).pretty()
Display the departments which are neither location in Seattle nor
Houston.
db.department.find({DLocations:{$nin:
["Seattle","Houston"]}}).pretty()
Display the projects in which employee 888665555 and 333445555
have worked.
db.project.find({"workerId":{$in:[" 888665555 ",
"333445555"]}}).pretty()
$all
The $all operator selects the documents where the value of a field is an
array that contains all the specified elements. To specify an $all
expression, use the following prototype:
{ <field>: { $all: [ <value1> , <value2> ... ] } }
Display the department which is located in Seattle, and Houston.
db.department.find({ "DLocations": {"$all":
[ "Seattle", "Houston"]}}).pretty()
NOTE: The $all is equivalent to an $and operation of
the specified values; i.e. the following statement:
db.department.find({$and:[{"DLocations":"Seattle"},
{ "DLocations":"Houston"}]}). pretty()

WiDS Workshop- Introduction to NoSQL MongoDB Page 21 of 41


db.department.find({DLocations:
["Seattle","Houston"]}).pretty()
$elemMatch
The $elemMatch operator matches documents that contain an array field
with at least one element that matches all the specified query criteria.
{<field>: { $elemMatch: { <query1>, <query2>, ... } } }

Display the employees who have at least one of the male dependents.
db.employee.find({ "Dependents": { "$elemMatch":
{ "DepGen": "M" }}}). pretty()
Count the employees who have at least one of the male dependents.
db.employee.find({ "Dependents": { "$elemMatch":
{ "DepGen": "M" } }}).count()
Display the employees who have at least one of the female
dependents.
db.employee.find({ "Dependents": { "$elemMatch":
{ "DepGen": "F" }}}). pretty()
$regex
Provides regular expression capabilities for pattern matching strings
in queries.
Syntax:
{ <field>: { $regex: /pattern/, $options: '<options>' } }

Patterns can include anchors ^ to match the beginning and $ to match the
end of the string
Display documents of employees whose last name starts with the
letter Ja.
db.employee.find({ "LName": { $regex:
/^Ja/ } }).pretty()
Display documents of employees whose last name ends with the
letter N.

WiDS Workshop- Introduction to NoSQL MongoDB Page 22 of 41


db.employee.find({ "LName": { $regex:
/ong$/ } }).pretty()
Display documents of employees whose last name contain the string
‘rv’.

db.employee.find({ "LName": { $regex:


/rv/ } }).pretty()
Projection Document to Collection
Retrieve only the field names mentioned as 1.

Syntax:
db.collection_name.find([query_document],
[projection_document])

query_document= Fname:”Jon”
projection_document={Fname:1,_id:0}

Display the first and last name of all employees


db.employee.find({},{Fname:1, LName:1})
NOTE: {} means no criteria
Display the first and last name of the employee whose first name is
Jon
db.employee.find({Fname: "Jon"},{Fname:1, LName:1})
Display the first and last name of the employee whose first name is
Jon. Do not display the _id field.
db.employee.find({Fname: "Jon"},{Fname:1,LName:1,_id:0})
Display the employee info except address whose first name is Jon.
db.employee.find({Fname: "Jon"},{Address:0})
Display employee _id, first, last name and the gender ‘F” and the
dependents' name

WiDS Workshop- Introduction to NoSQL MongoDB Page 23 of 41


db.employee.find({Gender:"F"},{Fname:1,LName:1,
Gender:1,Dependents:{DepName:1}})
Display employee _id, first, last name and the gender, the dependents'
name and relation.
db.employee.find({Sex:"F"},{Fname:1,LName:1,
Sex:1,"Dependents.DepName":1,"Dependents.Relation":1})
Display employee _id, first, last name and the gender. Suppress the
dependents' info.
db.employee.find({Gender:"F"},{Fname:1,LName:1,
Gender:1},{"Dependents.DepName":0})
db.employee.find({Gender:"F"},{Fname:1,LName:1,
Gender:1},{"Dependents":0})

Display the first and last name of the employee whose first name is
Jon and the dependents information. Do not display the _id field.
db.employee.find({Fname: "Jon"},{Fname:1,
LName:1,Dependents:1,_id:0}).pretty()
Display the name of the employees whose last name contains letters
"ar".
db.employee.find({"LName" : {$regex : "ar"}}).pretty()
Relationships: Relationships in MongoDB represent how various
documents are logically related to each other. Relationships can be
modeled via two approaches
Embedded approach
Referenced approach.
Such relationships can be either 1:1, 1: N, N:1 or N:N.
Embedded Approach:
db.employee.insertOne({
_id:"888665584",

WiDS Workshop- Introduction to NoSQL MongoDB Page 24 of 41


Fname:"James",
Minit:"E",
LName:"Borg",
dBirth:"10-Nov-2007",
Address: "450 Stone Houston TX",
Gender: "M",
Salary:55000,
Department: {Dnumber:"5", Dname:"Research",
MgrSSn:"333445555", MgrStDate:"09-JAN-2016",
DLocations:["Houston","Chicago", "Seattle"]},
Dependents:[{DepName:"Ann", DepGen:"F",
Relation:"Daughter"}]
})
This approach maintains all the related data in a single document, which
makes it easy to retrieve and maintain. The whole employee document can
be retrieved in a single query.
The drawback is that if the embedded document keeps on growing too
much in size, it can impact the read/write performance
Referenced approach.
db.employee.insertOne(
{
_id:"888665555",Fname:"James", Minit:"E", LName:"Borg",
dBirth:"10-Nov-2007",Address: "450 Stone Houston TX",
Gender: "M", Salary:55000,
Dnumber:"5", Dependents:[{DepName:"Ann", DepGen:"F",
Relation:"Daughter"}]
})

WiDS Workshop- Introduction to NoSQL MongoDB Page 25 of 41


db.department.insertOne(
{_id:"5",Dname:"Research",MgrSSn:" 333445555",
MgrStDate:"09-JAN-2016",
DLocations:["Houston",”Chicago” "Seattle"]}

As shown above, the employee document contains the string field


Dnumber which contains department number. Using this Dnumber, we
can query the department collection and get department details from
there. With this approach, we will need two queries: first to fetch the
Dnumber fields from employee document and second to fetch these
department details from department collection.
$lookup (aggregation)
Performs Equality Match with a Single Join Condition
To perform an equality match between a field from the input documents
with a field from the documents of the "joined" collection, the $lookup stage
has this syntax:
db.collection_name.aggregate([{
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from"
collection>,
as: <output array field>
}
}
])

Reference:
https://www.mongodb.com/docs/manual/reference/operator/
aggregation/lookup/

WiDS Workshop- Introduction to NoSQL MongoDB Page 26 of 41


To display the information about the employee and the department in
which he/she is working.
NOTE: Dnumber is string field in employee collection
db.employee.aggregate([{
$lookup: {
from: "department",
localField: "Dnumber",
foreignField: "_id",
as: "Department Information"
}
}]).pretty()

SQL
SELECT * FROM employee, department
WHERE employee.Dnumber = department._id

NOTE: The MongodB commands are case sensitive.


To display the information about the project and the employee who is
working in each project.
NOTE: worker is an array of object fields in Project Collection
db.project.aggregate([{
$lookup: {
from: "employee",
localField: "worker.WorkerId",
foreignField: "_id",
as: "Employee Information"
}

WiDS Workshop- Introduction to NoSQL MongoDB Page 27 of 41


}]).pretty()

To display the information about the project and the department


which is controlling it.
NOTE: ConDep is an string field in Project Collection
db.project.aggregate([{
$lookup: {
from: "department",
localField: "ConDep",
foreignField: "_id",
as: "Department Information"
}
}]).pretty()

Aggregate Pipelines
Aggregation operations group values from multiple documents together and
can perform a variety of operations on the grouped data to return a single
result. In SQL count (*) and with group by is an equivalent of MongoDB
aggregation.
The aggregate () Method
For the aggregation in MongoDB, you should use aggregate () method.
Syntax
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Display female employee documents whose salary is greater than
30000
db.employee.aggregate([
{ $match : { Gender : "F", Salary : {$gt:30000 }} }
]).pretty()

WiDS Workshop- Introduction to NoSQL MongoDB Page 28 of 41


Display the project documents controlled by department 4 and
located in Houston.
db.project.aggregate([
{ $match : { ConDep : "4", Plocation : "Houston" } }
]).pretty()

Display the project documents controlled by department 4 and


located either in Houston or Seattle.
db.project.aggregate([
{ $match : { ConDep : "4", Plocation: {$in:
[ "Houston", "Seattle"]} } }
]).pretty()

Display female employee’s first, last, salary whose salary is greater


than 30000
db.employee.aggregate([{ $project : { _id : 0, Fname :
1, Lname : 1,Sex : 1, Salary : 1}},
{$match : { Sex : "F", Salary : {$gt:30000 }}}
]).pretty()

OR
db.employee.aggregate([{$match : { Gender : "F", Salary
: {$gt:30000 }}},
{ $project : { _id : 0, Fname : 1, Lname : 1,Gender:1,
Salary:1}} ]).pretty()

Display Project Name, locations and the controlling department’s


number.
Db.project.aggregate([
{ $project : { _id : 0, Pname : 1, Plocation : 1,
ConDep : 1 } }
]).pretty()

WiDS Workshop- Introduction to NoSQL MongoDB Page 29 of 41


Display the total salary, maximum salary, minimum salary and total
number of employees iin a company
db.employee.aggregate([{
$group : {
_id : null,
totalSal: { $sum: "$Salary"},
averageSalary: { $avg: "$Salary" },
minSalary: { $min: "$Salary" },
maxSalary: { $max: "$Salary" },
count: { $sum: 1 }

}
}
])
Display the total salary, maximum salary, minimum salary and total
number of employees in each department

db.employee.aggregate([{
$group : {
_id : "$Dnumber",
totalSalary: { $sum: "$Salary"},
averageSalary: { $avg: "$Salary" },
minSalary: { $min: "$Salary" },
maxSalary: { $max: "$Salary" },
count: { $sum: 1 }

WiDS Workshop- Introduction to NoSQL MongoDB Page 30 of 41


}
}
])

Note:
When used in the $group stage, $sum has the following syntax
{$sum: < expression > }
and returns the collective sum of all the numeric values that result from
applying a specified expression to each document in a group of documents
that share the same group by key.
It will aggregate a value of one for each document in the group, thus
yielding the total number of documents per group.
Expression 1 to a document will return 1, since the expression will apply to
each document in the group, so {$sum: 1} will return the amount or number
of documents in the group.

Display the department number and the total, maximum, minimum


and average salary in each department in descending order.
db.employee.aggregate([
{ $group : { _id : "$Dnumber",
totalSalary : { $sum : "$Salary" },
maxSalary : { $max : "$Salary" },
minSalary : { $min : "$Salary" },
avgSalary : { $avg : "$Salary" },
count: { $sum: 1 }} },
{ $sort : { totalSalary : -1 } }
]).pretty()

WiDS Workshop- Introduction to NoSQL MongoDB Page 31 of 41


Display the department number and the total number of projects it is
managing
db.project.aggregate([
{ $group : { _id : "$ConDep", NoOfProject : { $sum :
1 } } }
]).pretty()

Display the department number and the total number of projects it is


managing in a new collection.
aggResults.

Db.project.aggregate([
{ $group : { _id : "$ConDep", totaldocs : { $sum :
1 } } },
{ $out : "aggResults" }
])
----------
db.aggResults.find().pretty()
Display employee Franklin’s records and show separately the record
for his dependents
db.employee.aggregate([
{ $match : { Fname : "Franklin" } },
{ $unwind : "$Dependents" }
]).pretty()
Display the total number of dependents for all the employees
db.employee.aggregate([
{ $unwind : "$Dependents" },
{ $count : "total_documents" }

WiDS Workshop- Introduction to NoSQL MongoDB Page 32 of 41


]).pretty()
Display the project id and the total number of hours spent by all the
workers.
db.project.aggregate([
{ $unwind : "$worker" },
{ $group : { _id : "$_id", totalHours : { $sum :
"$worker.Hours" } } }
]).pretty()
Display the project id and the average number of hours spent by the
workers. The documents should be sorted in descending order of the
average hours.
db.project.aggregate([
{ $unwind : "$worker" },
{ $group : { _id : "$_id", averageHours : { $avg :
"$worker.Hours" } } }, {
$sort: { "averageHours": -1 }
}
]).pretty()
Display the number of employees gender wise. (shortcut method-
#sortByCount)
db.employee.aggregate([
{ $sortByCount : "$Gender" }
]).pretty()
Update Commands
The update() method updates the values in the existing.
Syntax:
db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

WiDS Workshop- Introduction to NoSQL MongoDB Page 33 of 41


To update the department name of the Administration to
Administraive Affairs for Department number =4
.db.department.updateOne(

{_id:”4”},{ $set: {Dname:”Administrative Affairs”}})


To update the salaries of all female employees
db.employee.updateMany(
{gender:”F”},{ $set: {Salary:80000}})
However this query does not work because gender is written instead of
Gender.. MongoDB is case sensitive.
Db.employee.updateMany(
{Gender:”F”},{ $set: {Salary:80000}})
Delete Commands
MongoDB’s delete() method is used to remove a document from the
collection.
Syntax:
db.COLLECTION_NAME.delete(SELECTION_CRITERIA)

Delete one record


db.employee.deleteOne( {_id:”999887777”})
The record for Alicia has been deleted
We can add this record again.
Db.employee.insertOne(
{_id:”999887777”,Fname:”Alicia”, Minit:”J”,
Lname:”Zelaya”,
dBirth:”19-JUL-1995”,Address: “3321 Castle, Spring,
TX”, Gender: “F”, Salary:25000,
Dnumber:”4”,
Dependents:

WiDS Workshop- Introduction to NoSQL MongoDB Page 34 of 41


[
{DepName:”Mary”, DepGen:”F”, Relation:”Daughter”},
{DepName:”Sally”, DepGen:”F”, Relation:”Daughter”}]
})
Delete many records
db.employee.deleteMany({Gender:”M”})

WiDS Workshop- Introduction to NoSQL MongoDB Page 35 of 41


VIII. Appendix – A

MongoDB Operators
To query the document on the basis of some condition, you can use following operations.

Operatio Syntax Example RDBMS


n Equivalent

Equality {<key>:{$eg;<value>}} db.mycol.find({“by”:”tutorials where by =


point”}).pretty() ‘tutorials point’

Less Than {<key>:{$lt:<value>}} db.mycol.find({“likes”:{$lt:50}}).pretty() where likes <


50

Less Than {<key>:{$lte:<value>}} db.mycol.find({“likes”:{$lte:50}}).pretty() where likes <=


Equals 50

Greater {<key>:{$gt:<value>}} db.mycol.find({“likes”:{$gt:50}}).pretty() where likes >


Than 50

Greater {<key>:{$gte:<value>}} db.mycol.find({“likes”: where likes >=


Than {$gte:50}}).pretty() 50
Equals

Not {<key>:{$ne:<value>}} db.mycol.find({“likes”:{$ne:50}}).pretty() where likes !=


Equals 50

Values in {<key>:{$in:[<value1>, db.mycol.find({“name”:{$in:[“Raj”, Where name


an array <value2>,……<valueN>]}} “Ram”, “Raghu”]}}).pretty() matches any of
the value in :
[“Raj”, “Ram”,
“Raghu”]

Values {<key>:{$nin:<value>}} db.mycol.find({“name”:{$nin:[“Ramu”, Where name


not in an “Raghav”]}}).pretty() values is not in
array the array :
[“Ramu”,
“Raghav”] or,
doesn’t exist at
all

WiDS Workshop- Introduction to NoSQL MongoDB Page 36 of 41


IX. Appendix – B
Functions used in Aggregate method

Expressio Description Example


n

Sums up the defined value Db.mycol.aggregate([{$group : {_id :


$sum from all documents in the “$by_user”, num_tutorial : {$sum : “$likes”}}}])
collection.

Calculates the average of all Db.mycol.aggregate([{$group : {_id :


$avg given values from all “$by_user”, num_tutorial : {$avg : “$likes”}}}])
documents in the collection.

Gets the minimum of the Db.mycol.aggregate([{$group : {_id :


$min corresponding values from all “$by_user”, num_tutorial : {$min : “$likes”}}}])
documents in the collection.

Gets the maximum of the Db.mycol.aggregate([{$group : {_id :


$max corresponding values from all “$by_user”, num_tutorial : {$max : “$likes”}}}])
documents in the collection.

Inserts the value to an array in Db.mycol.aggregate([{$group : {_id :


$push the resulting document. “$by_user”, url : {$push: “$url”}}}])

Inserts the value to an array in Db.mycol.aggregate([{$group : {_id :


$addToSet the resulting document but “$by_user”, url : {$addToSet : “$url”}}}])
does not create duplicates.

Gets the first document from Db.mycol.aggregate([{$group : {_id :


the source documents “$by_user”, first_url : {$first : “$url”}}}])
according to the grouping.
$first Typically this makes only
sense together with some
previously applied “$sort”-
stage.

Gets the last document from Db.mycol.aggregate([{$group : {_id :


the source documents “$by_user”, last_url : {$last : “$url”}}}])
according to the grouping.
$last Typically this makes only
sense together with some
previously applied “$sort”-
stage.

WiDS Workshop- Introduction to NoSQL MongoDB Page 37 of 41


X. Appendix – C
Aggregation pipelines

MongoDB is a database management system that allows you to store large


amounts of data in documents that are held within larger structures known
as collections. You can run queries on collections to retrieve a subset of
documents matching given conditions, but MongoDB’s query mechanism
doesn’t allow you to group or transform the returned data. This means your
options for performing meaningful data analysis with MongoDB’s query
mechanism alone are limited.

As with many other database systems, MongoDB allows you to perform a


variety of aggregation operations. These allow you to process data records
in a variety of ways, such as grouping data, sorting data into a specific
order, or restructuring returned documents, as well as filtering data as one
might with a query.

MongoDB provides aggregation operations through aggregation


pipelines — a series of operations that process data documents
sequentially. In this tutorial, you’ll learn by example how to use the most
common features of the aggregation pipelines. You’ll filter, sort, group, and
transform documents, and then use all these features together to form a
multi-stage processing pipeline.

Understanding Aggregation Pipelines

When working with a database management system, any time you want to
retrieve data from the database you must execute an operation known as
a query. However, queries only return the data that already exists in the
database. In order to analyze your data to find patterns or other information
about the data — rather than the data itself — you’ll often need to perform
another kind of operation known as an aggregation.

Aggregations group data from multiple sources and then process that data
in some way to return a single result. In a relational database, the database
management system will typically pull data from multiple rows in the same
table to execute an aggregate function. In a document-oriented database
like MongoDB, though, the database will pull data from multiple documents
in the same collection.

WiDS Workshop- Introduction to NoSQL MongoDB Page 38 of 41


MongoDB enables you to perform aggregation operations through the
mechanism called aggregation pipelines. These are built as a sequential
series of declarative data processing operations known as stages. Each
stage inspects and transforms the documents as they pass through the
pipeline, feeding the transformed results into the subsequent stages for
further processing. Documents from a chosen collection enter the pipeline
and go through each stage, where the output coming from one stage forms
the input for the next one and the final result comes at the end of the
pipeline.

It may help to think of this process like vegetables going through an


assembly line in a restaurant kitchen. In this analogy, vegetables go
through a set of stations, each responsible for a single action: washing,
peeling, chopping, cooking, and plating. Likewise, data entering an
aggregation pipeline must go through several stages, each of which are
responsible for a specific operation.

Pipeline Concept
In UNIX command, shell pipeline means the possibility to execute an operation on
some input and use the output as the input for the next command and so on. MongoDB
also supports same concept in aggregation framework. There is a set of possible
stages and each of those is taken as a set of documents as an input and produces a
resulting set of documents (or the final resulting JSON document at the end of the
pipeline). This can then in turn be used for the next stage and so on.
Following are the possible stages in aggregation framework –
 $project – Used to select some specific fields from a collection.
 $match – This is a filtering operation and thus this can reduce the amount of
documents that are given as input to the next stage.
 $group – This does the actual aggregation as discussed above.
 $sort – Sorts the documents.
 $skip – With this, it is possible to skip forward in the list of documents for a given
number of documents.
 $limit – This limits the amount of documents to look at, by the given number
starting from the current positions.
 $unwind – This is used to unwind document that are using arrays. When using
an array, the data is kind of pre-joined and this operation will be undone with this
to have individual documents again. Thus with this stage we will increase the
amount of documents for the next stage.

Using aggregation method, various stages can perform operations on data


such as:

WiDS Workshop- Introduction to NoSQL MongoDB Page 39 of 41


 filtering: This resembles queries, where the list of documents is
narrowed down through a set of criteria
{$match:{} }
 sorting: You can reorder documents based on a chosen field
{sort” {fieldname”:-1} }
 transforming: The ability to change the structure of documents
means you can remove or rename certain fields, or perhaps rename
or group fields within an embedded document for legibility
{ $project : { fieldname1 : 0, fieldname2 : 1,….} }
 grouping: You can also process multiple documents together to form
a summarized result
{group: {“_id”: “”field_name”} }

Pipeline stages do not need to produce the same number of documents


they receive. Stages can generate new documents or filter out existing
ones from the collection that was entered into the start of the pipeline.

WiDS Workshop- Introduction to NoSQL MongoDB Page 40 of 41

You might also like