MongoDB Lab
MongoDB Lab
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
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
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
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.
DepName
Relation DepSec
‘Ann’
‘Daughter’ ‘F’
{_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:
{_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:"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"},
{_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",
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.
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 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",
Reference:
https://www.mongodb.com/docs/manual/reference/operator/
aggregation/lookup/
SQL
SELECT * FROM employee, department
WHERE employee.Dnumber = department._id
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()
OR
db.employee.aggregate([{$match : { Gender : "F", Salary
: {$gt:30000 }}},
{ $project : { _id : 0, Fname : 1, Lname : 1,Gender:1,
Salary:1}} ]).pretty()
}
}
])
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 }
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.
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" }
MongoDB Operators
To query the document on the basis of some condition, you can use following operations.
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.
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.