MONGODB
The Database for the modern world
Data Vs Information
Data is any set of characters that is gathered
and translated for some purpose.
DATA INFORMATION
Raw facts Processed data
No context ( Meaningless) Data with context
Just numbers and characters Value added to data
Not organized Organized, Summarized and Analyzed
Importance of data
✔ Optimize business operations.
✔ Better decision making.
✔ Improved marketing strategies.
✔ Reduce costs.
✔ Increase revenue and profit.
Big Data
✔ Collection of data that is huge in volume.
✔ Growing exponentially with time
✔ Contains wide variety of data types.
✔ Can be structured or unstructured.
Examples of Big data
✔ The New York Stock Exchange is an
example of Big Data.
✔ Facebook generates 4+ petabytes of data
per day.
✔ A small jet plane generates 10+TB of data within 30 minutes of
flight.
✔ Google generates 20 petabytes of data per day
And much more….
Types of Big Data.
Big data can be categorized into three types.
1) Structured data.
2) Unstructured data.
3) Semi structured data.
Structured data.
Any data that can be stored, accessed and processed in the form of
fixed format (where the format is well known in advance) is termed
as a ‘structured’ data.
Normally stored in the form of
tables.
Example: Employee details
Unstructured data.
Any data with unknown form or the structure is classified as
unstructured data. In addition to the size being huge, unstructured data
poses multiple challenges in terms of its processing for deriving value
out of it
Ex: text documents, media files,
output returned by google
etc,
Semi structured data.
Semi-structured data can contain both the forms of data. We can see
semi-structured data as a structured in form but it is actually not
defined.
Example:
<rec><name>PrashantRao</name><sex>Male</sex><age>35</
age
></rec> <rec>
<name>Seema
R.</name><sex>Female</sex><age>41</age></rec> <rec>
<name>Satish
Mane</name><sex>Male</sex><age>29</age></rec> <rec>
Characteristics of Big data.
4 Vs of big data
➔ Volume: enormous size
➔ Velocity: Speed of data generation
➔ Variety: Heterogeneous nature of
nature. (posts in social media)
➔ Veracity : Accuracy and Quality of data.
NoSQL Database
Can you imagine using SQL to work with this volume of data?
Why should we use NoSQL databases?
▪Data generated at a very high speed need to be stored
and processed efficiently.
▪ Data cannot be managed in using traditional databases.
▪NOSQL is a Non-relational database which provides a
mechanism for storage of data that does not require a fixed schema.
▪ Most of the NoSQL databases are open source.
Advantages
◆ Non relational and schema less data model.
◆ Highly scalable. Supports horizontal scaling.
◆ Ability to handle change over time.
◆ No reliance on SQL magic.
◆ Less need for ETL
Types of NoSQL Database
✔ Key-Value Store : Simple model to store Values.
Ex: Redis, Dynamo, Raik etc.
✔ Column-Based : Every column is treated
separately Ex: Hbase, Cassandra,
hypertable value
✔ Document Oriented: Stores and retrieves data as Key-
pair. Document is stored in JSON format.
EX: MongoDB, CouchDB etc..
✔ Graph-based : Stores the entities and their relations
Ex: Neo4J, orientDB etc..
Issues with traditional RDBMS
Any Guess ?
Scalability
● Data is stored in multiple tables.
● Difficult to scale huge volume of data.
Flexibility
● Fixed data structure.
● Not easy to make modifications in data
structures.
● Consumes lot of time in design
CAP
THEOREM
✔ Desirable properties or guarantee of
distributed database system.
✔ Can achieve at most two out of three
guarantees for a database.
✔ Three guarantees
a. Consistency : All clients see same
data at same time.
b. Availability : system should always perform reads/writes on
any non-failing node of the cluster successfully
c. Partition tolerance: System continues to function in spite of
failure of a node.
CAP
THEOREM
MONGODB
▪ Open source, cross- platform Document DB
▪ Scalable and very flexible.
▪ Name derived from the word humongous means “Huge And
enormous.
▪ It is a NOSQL Database developed by MongoDB Inc.
▪ Stores data in collections as JSON documents
▪ Does not have tables , rows and columns.
▪ Does not have a fixed schema.
Advantages
● Easy to use: More Flexible than RDBMS No predefined schema is
required.
● Easy to scale: Easy to upgrade the server to a bigger one with more
resources (CPU, RAM).
● It allows to split data across different servers. It automatically manages
the load balancing across the cluster
● Rich features: It supports features like
✔ Indexing
✔ Aggregation
● High Performance: It is designed to maintain the high performance
RDBMS vs MONGODB
RDBMS MongoDB
Database Database
Table/relation Collection
Row/record
column/attribute Document
Field
JSON Sample
Applications
▪ E-commerce product catalogue.
▪ Big Data
▪ Content management.
▪ Social websites.
▪ Geolocations.
MongoDB Server
MongoDB server will be installed in Windows as a service
The Default address
[Link] with port number
27017.
MongoShell : Command line client of
MongoDB server.
Installed as
[Link].
Getting started
MongoDB Processes
1. Mongod: Stands for Mongo daemon.
Background process used by mongoDB
Manages all server related tasks
like accept request, managing memory
etc..
2. Mongo : Command line shell that can
interact with server.
Where data is stored : Default directory in mongodb
to store is data
Architecture of MongoDB
The key components of MongoDB are
Database: Storage of data. Each database has its own set of files .
A single server can have more than one database.
Collections: A group of documents. Equivalent to table in RDBMS
Documents: Basic unit of [Link] is a set of key value pair. Equivalent
to rows in RDBMS.
Working With MongoDB
Find the Current database.
>db
Find the list of databases in the server
>show dbs
Choose a particular database
>use Database_name
Creating a database.
>use database_name
If the database with the above name does not exist then a new
database will be created. Else will navigate the database with the
given name.
Data Modeling
✔ The process of defining how data is stored and what relationships exist
between different collections in our database.
✔ It also seeks to represent the data’s organization and grouping.
Two Types :
1. Embedded data model or Denormalized model.
2. Referenced data model or Normalized model.
Data Modelling
Use Case :
▪ A client who needs a database design for his website. His website
has the following requirements:
▪ Every post is distinct (contains unique title, description and url).
▪ Every post can have one or more tags.
▪ Every post has the name of its publisher and total number of likes.
▪ Each post can have zero or more comments and the comments
must contain user name, message, data-time and likes.
Data Modelling
For the above requirement, a minimum of three tables are required in
RDBMS.
Data Modelling
In MongoDB, schema design will have one collection post and has the
following structure:
{ _id: POST_ID title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [ { user: 'COMMENT_BY', message: TEXT, datecreated:
DATE_TIME, like: LIKES },
{ user: 'COMMENT_BY', message: TEST, dateCreated: DATE_TIME,
like: LIKES }}}
Referenced data model
□Refer the sub documents in the original document
🡪 using references.
Example:
POST
{Id:”p1”,title: “mongoDB”, description: “tutorial” ,
url:”[Link]” , likes:”10”}
COMMENTS
{comment_id:”c1”,post_id:”p1”,user:”John”,
TAG {tag_id:”t1”, post_id:”p1” tag:”mongo”}
comment:Good”,likes:”5”}
S
Embedded data model
□ All related data will be embedded in a single
document. Example:
{ _id: “P1”, title: MongoDB,
description: “Introduction to mongoDB”,
by: “Prakash”,
url: “www,[Link]”, tags:
[“Intro”, “Basics”, “CRUD”],
likes: 10,
comments: [ { user: “Basha”, message: “Nice”, datecreated:
new date(“25-05-2022”), like: 5 },
{ user: ‘Suresh', message: “Good”, dateCreated: new
date(“03-06-2022”), like: 4 }]}}
Collections in
MongoDB
□ Collection is the basic building block which is agroup of
documents. Documents can have different fields. Equivalent to
table in RDBMS.
Two types of collections
1) Normal collection: Holds a normal collection
2) Capped collection : A fixed size collection that automatically
overwrites its oldest entries when it reaches the max. specified
size.
Normal collections
Can be created in two ways
1. Empty collection
> [Link](“name”)
2. Using Insert command
> [Link]({“key:value”….})
List the collections in the database
> Show
collections Get the
field names
Capped collections
Capped collections are fixed size collections which automatically
overwrites the oldest entries
[Link](“name”,
{capped:true,size:614280,max:100} )
Note: Older documents will be removed if the collection reaches
the max. Size limit before it reaches the max. document
count
[Link]() method is used to check.
Altering collections
Add fields
a) New field without value
[Link]({},{$set:{new_field:null}})
b) New Field with value
[Link]({},{$set:{new_field:”value”}})
Add Multiple fields
[Link]({},{$set:{new_field1:null,new_field2:
null}})
Altering collections
Remove fields
a) Remove one field
[Link]({},{$unset:{fieldname:1}})
b) Remove multiple fields
[Link]({},{$unset:{field1:1,field2:1}})
c) Remove a sub field
[Link]({},{$unset:{“[Link]”:1}})
Altering collections
Rename fields
a) Rename one field
[Link]({},{$rename:{oldname:newname}})
false-> upsert false , true: multi true(all documents)
b) Rename multiple fields
[Link]({},{$rename:{oldname1:newname1,
oldname2,newname2}})
c) Rename sub fields
[Link]({},{$rename:{“[Link]”:”[Link]”}})
Drop Collections
The [Link]() method is used to drop a
collection from the database.
Example:
>[Link]()
MongoDB Documents
□Equivalent to a row in RDBMS. Each document has multiple fields
which are similar to columns in RDBMS.
□ Can contain array and embedded document in it.
□ MongoDB drivers convert JSON document to BSON data
Example :
{ "firstName": "John",
"email":
"[Link]@[Link]", "salary":
"33000",
"DoB": new Date('Mar 24,
2011'),
"skills": [ "Angular", "React", "MongoDB"
], "address":
{ "street":"Upper
Street", "house":"No 1",
"city":"New York",
Insert Documents
▪Can insert documents to a new collection or to a
collection that has been already created.
▪ Save() method can also be used
▪ Three insert methods for inserting data.
1. insertOne(): Insert single document only.
2. insertMany: Insert more than one
document.
3. insert() : Insert one or more documents
Data types
string store string values
integer Store numerical values
Double Store Floating point values
Date Store current date or time in Unix format
Arrays Store multiple values in one key
Insert Single Document
[Link]({key:”value”,……}) will insert
a single document to the collection.
Example:
[Link] (
{ firstName:
"John",
lastName: "King",
email:
"[Link]@ab
[Link]"
Insert multiple Documents
Insert() or InsertMany() methods are
used to insert multiple values.
[Link] ([{key:”value”,
……},{key:”value”,……}]). Example:
[Link] ( [
{ firstName: "John", lastName: "K", email:
"[Link]@[Link]" },
{ _id:2,firstName: "Sachin", lastName: "T", email:
"sachin.t@[Link]" },
{ firstName: "James", lastName: "B", email:
"jamesb@[Link]" }
]
)
Update Documents
The update() method is used to update the values in a
collection.
Methods of updating the document
1. update()
2. save()
3. updateOne()
4. updateMany()
Update Documents
The update() method is used to update the first available value in
a collection.
Syntax:
>db.COLLECTION_NAME.update(SELECTION_CRITERIA,
UPDATED_DATA)
Example:
> [Link]({‘ename':‘Smith'},{$set:{‘salary‘:’7000'}})
Update Documents
The save() method replaces the existing document with the new
document passed in the save() method.
Syntax:
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example:
>[Link]( { "_id" : ObjectId("507f191e810c19729de860ea"),
“enam":”James", “salary":”5000“,deptno:”10” } )
Update Documents
The findOneAndUpdate() method updates the first matched document
in the collection that matches the selection criteria. If more than one
document matched the selection criteria then it updates only the first
matched document.
Syntax:
>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITER
IA, UPDATED_DATA)
Example:
□[Link]( {First_Name: 'Ramesh'},
{ $set: { Age: '30',e_mail: 'radhika_newemail@[Link]'}} )
Update Documents
The updateMany() method updates all the documents that matches
the given filter.
Syntax:
>db.COLLECTION_NAME.update(<filter>, <update>)
Example:
> [Link]( {Age:{ $gt: "25" }}, { $set: { Age:
40'}}
)
Upsert
Combination of update and insert (update + insert = upsert).
If set to true and the document or documents found that match the
specified query, then the update operation will update the
matched document or documents.
If the value of this option is set to true and no document or documents
matches the specified document, then this option inserts a new
document in the collection and this new document have the fields that
are indicated in the operation.
Example: [Link]({query:
{name:"Ram"}, update:{$set:
{department:"Development"}}, upsert:true})
Delete Documents
The remove () method is used to delete the documents from the collection. Remove()
method accepts two parameters. One is deletion criteria and second is justOne flag.
Syntax:
>db.COLLECTION_NAME.remove(DELLETION_CRITERIA)
Example:
>[Link]({‘sal':’5000'}) 🡪 Remove all the documents whose salary is 5000.
Remove Only One
If there are multiple records and you want to delete only the first record, then
set justOne parameter in remove() method.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
Remove All Documents : no crireria is specified
> [Link]({})
Query document
Query in a database system is a command that is used for extracting data
from a database in a readable format.
The find() method is used to query the data from the collection
Syntax:
[Link]()
find() method will display all the documents in a non-structured way.
Use pretty() method to display the result in formatted way.
[Link]().pretty()
findOne() method is used to return only one document. If multiple
documents satisfy the condition then this will return only the first document.
>[Link]()
> [Link]({name:"Smartphone"})
Projection in MongoDB
Projection in MongoDB simply means selecting specific fields to
return from the query.
Syntax: [Link]({},
{field1:boolean,field2:boolean}) [Link]({ },
{_id:0,"[Link]":1,name:1})
.pretty()
Note : 1 for true and 0 for false.
Limit and skip methods
Limit the no. of documents to be returned by the query. It accepts
one number type argument.
Syntax:
[Link]().limit(n)
Skip is used to skip the [Link] [Link] accepts one number
type argument.
Syntax:
[Link]().limit(n).skip(n)
Sorting the output
The Sort() method is used to sort the output .
It accepts the list of fields along with the sort order.
[Link]().sort({key:1})
Note : 1 for Ascending order
And -1 for descending order.
Querying last N records
Find the least 3 salary getter in the organization
[Link]().sort({salary:-1}).skip([Link]()-3).pretty()
Find the 3rd junior most employee in the organization.
[Link]().sort({hiredate:-1}).skip([Link]()-3).limit(1).pret
ty()
Distinct values
The Distinct method is used to find the distinct values of a field in
the collection
[Link]("item")
To count the number of distinct values
[Link]("item").length
Query operators
Used to query the documents based on some conditions
OPERATION OPERAND EXAMPLE
Equality $eq [Link]({({name:"Smartphone"})})
Less than $lt [Link]({price:{$lt:1000}}).pretty()
Less than equals $lte [Link]({price:{$lte:1000}}).pretty()
Greater than $gt [Link]({price:{$gt:1000}}).pretty()
Greater than
$gte [Link]({price:{$gte:1000}}).pretty()
equals
Not equals $ne [Link]({name:{$ne:”Smartphone”}}).pretty()
Values in an
$in [Link]({price:{$in:[1000,670,699]}})
array
Query Embedded
Document
Specify criteria for embedded documents using Dot notation
Ex: [Link]({“[Link]”:”Coimbatore”})
Query Array Element
Find documents based on array element , Index or size
Ex:
[Link]({“skills”:”Java”})
→ Returns documents which contains Java
[Link]({“skills”:{$in:[”Java”,”SQL”]}})
→ Returns documents which contains Java or SQL
[Link]({“skills”:{$all:[”Java”,”SQL”]}})
→ Returns documents which contains Java and SQL
[Link]({“skills”:{$size:3})
→ Returns documents where skills contains
Logical operators
Allow us to be more granular in our search for data
Used when multiple conditions are to be met.
Types:
$AND : Match all the specified query clauses.
$OR : At least one one of the query clauses
matches.
$NOR : Failed to match both the clauses.
$NOT: Conditions must be false.
Syntax : {operator :[{condition1},{condition2}.....]}
AND operator
Used to test multiple conditions. If both the conditions are
true then that document will be selected. It is the default
operator.
Syntax:
>[Link]({ $AND: [ {<key1>:<value1>},
{ <key2>:<value2>} ] })
Example :
[Link]({job:"Clerk",deptno:30})
[Link]({$and:
[{job:"Salesman"},{salary:1500}]})
[Link]({$and:[{salary:{$gt:1250}},{salary:{$lt:3000}}]},{_id:0,
ename:1,salary:1}).pretty() [Link]({salary:
OR / NOR operator
Select the documents which satisfies at least one of query clauses.
Syntax:
>[Link]({ $OR: [ {<key1>:<value1>}, { <key2>:<value2>} ] })
Examples : [Link]({$OR:[{job:"Salesman"},
{deptno:10}]})
[Link]({$nor:[{job:"Salesman"},
{job:"Analyst"}]})
AND- OR Condition
Multiple conditions can be checked using the AND and OR operators.
Examples : [Link]({salary:{$lt:1250},$or:[{job:"Salesman"},
{job:"Analyst"}]})
[Link]({$and:[{$or:[{job:"Clerk"},{job:"Salesman"}]},
{$or:[{deptno:30},{deptno:20}]}]})
AND- OR Condition
Multiple conditions can be checked using the AND and OR
operators.
Example : [Link]({price:{$lt:1000},$or:
[{name:"mouse"},{name:"he ad phones"}]})
NOT Condition
Find the documents that do not match the given condition.
Get the name and salary of the employees whose salary is not
greater or equal than 1240.
Example : [Link]({salary:{$not:
{$gte:1240}}})
Element operators
Used to identify the documents using the field of the document.
$exists
$exists operator matches the documents that have the specific
field including null
Syntax: {{field:{$exist:boolean}}
Example :
Find the products where the price field exists.
[Link]({price:{$exist:true}}).pretty()
Element operators
$type
Matches documents according to the specified field type. These
field types are specified BSON types.
It can also accept multiple types
Syntax: {{field:{$type:BSON type}}
{{field:{$type:BSON type1,type2}}
Example :
Find the products where the price has a string value .
[Link]({price:{$type:”string”}}).pretty()
Evaluation operators
$mod
Calculate the remainder of division
Syntax: {{field:{$mod:[10,0]}}
Example :
Find the products where the price is divisible by 10 .
[Link]({price:{$mod[10,0]}}).pretty()
Regular expressions
Regular Expression ($regex): Provide a way to search a string
which matches a specified pattern.
Syntax: {{field:{$regex:’pattern’]}}
Example :
Find the names of employees which starts with A
[Link]({ename:{$regex:'^A'}}).pretty()
Find the names of employees which contains A
[Link]({ename:{$regex:'A'}}).pretty()
Find the names of employees which end with R
[Link]({ename:{$regex:'r$'}}).pretty()
[Link]({ename:{$regex:'l',$options:'i'}}).pretty()
i→ case insensitive
Pattern matching
without regex.
Use / / delimiters for pattern matching without $regex
Examples
[Link]({ename:/Ad/}).pretty()
[Link]({ename:/^A/}).pretty()
[Link]({ename:/r$/}).pretty()
Matching the Second character
[Link]({ename:/^.d/})
[Link]({ename:{$regex:/
^Ad.*/}}).pretty()
. stipulates any character and * denotes any no of characters
Array Operators
Array is nothing but a simple list of related values
$all : Matches arrays that contain all the elements in the specified
Query. [Link]({skills:{$all:["Oracle","Java"]}},
{_id:0,ename:1,skills:1}).pret ty()
$size: Used to select the the documents that have an array which
contains specified number of elements. [Link]({skills:
{$size:3}},{_id:0,ename:1,skills:1}).pretty()
[Link]({skills:{$size:3}},
{_id:0,ename:1,skills:1}).pretty().count()
$elemMatch: select the documents if the elements in the array fields
matches all the specified conditions. [Link]({storage:
{$elemMatch:{$eq:128}}},{name:1,storage:1})
Aggregation Pipeline
✔ Aggregation is the process of selecting data from a collection in
MongoDB and expressed in a customized form.
✔ It processes multiple documents and returns computed results.
✔ Used to provide statistical data for analysis,
✔ Helps to break the complex logic into a simple set of sequential
operations.
✔ Output from one stage is fed as an input for the next stage until
the desired result is achieved.
✔ Each stage of transformation during this process is termed as
pipeline.
Aggregation Pipeline
Aggregation Pipeline
Stages in Pipeline
$match() stage: Filters those documents those
satisfy the condition
$project(): Project the columns
$group() stage : Does the aggregation job.
$sort() stage: Sorts the resulting
documents Syntax:
[Link]([{$match…},{$group…},{$sort..}])
Note:
✔ The input of the pipeline can be one or several
conditions.
✔ There is no limit to the number of stages used in the query
How it works
Syntax:
[Link](<aggregate Operation>)
✔ Aggregation should be defined as an array. Each array
value is a stage.
Syntax: ( [ {Stage1},{Stage2},{Stage3} ] )
✔ It will pass the set of existing documents or it will generate
the computed result based on aggregation condition and
that result will be passed(piped)to the next stage.
Common stages in aggregation
$match: Filters the documents based on the given condition.
$project: Used to select specific field from the collection.
$group: Group the identical documents and does the actual
aggregation.
$sort: Sort the documents.
$skip : Skip the particular amount of documents.
$limit: Limit the no of documents for the next stage.
$unwind: used to unwind documents that are using arrays.
$Match
▪$match allows to filter the documents that match a defined set
of conditions.
▪Only matching values proceed to the next stage of the pipeline.
Syntax: {$match:{condition}}
Example:
>[Link]([{$match:{deptno:30}}])
[Link]([{$match:{deptno:30,job:"Salesman"}}])
>[Link]([{$match:{deptno:30,job:"Salesman",salary:{$gt
e:1500}}}]).pretty()
>[Link]([{$match:{deptno:30,job:"Salesman"}},{$project
:{_id:0,ename:1}}]).pretty()
$project
▪ Specify the fields that must be returned by the aggregation
▪ New computed fields can also be included.
Example: [Link]([{$project:{name:1,price:1,_id:0}}]).pretty()
[Link]([{$project:{_id:0,ename:1,deptno:1,salary:1,"new
salary":{$add:["$salary",1000]}}}]) [Link]([{$project:
{name:1,price:1,_id:0, tax:{$multiply:["$price",.025]}}}]).pretty()
[Link]([{$project:{_id:0,ename:1,job:1,comm:{$ifNull:["$co
mm",0]}}}]).pretty()
$group
▪Used to group identical documents based on a particular key.
Can use single or multiple fields
The Following operators are used
□$count
□$sum
□$avg
□$min
□$max
□$push
$count
Count the number of documents
Syntax: { $count: <string>}
** the string is the name of the output field which has the
count as its value
$ Addresses the field value.
> [Link]([{$count:”Employees”}])
Match and count [Link]([{$match:{deptno:30}},
{$count:"No of empl in deptno30"}])
[Link]([{$match:{comm:null}},{$count:"employees
earn commission"}])
$sum
>[Link]([{$group:{_id:"$deptno",”no. of
employees”:{$sum:1}}}])
>[Link]([{$group:{_id:"$deptno",salary:
{$sum:"$salary"}}}])
[Link]([{$group:{_id:{deptno:"$deptno",job:"$job"},total_sala
ry:{$sum:"$salary"}}}]).pretty()
$avg
Find the average salary in each dept.
[Link]([{$group:{_id:"$deptno","aver
age salary":{$avg:"$salary"}}}])
$max and $min
[Link]([{$group:{_id:null, maxsalary:{$max:"$salary"}}}])
[Link]([{$group:{_id:"$deptno",maxsalary:{$max:"$salary"}}}])
[Link]([{$group:{_id:null,minsalary:{$min:"$salary"}}}])
$push
$push : Appends a specified value to an array.
Syntax:
{ $push: { <field1>: <value1|condition>, ... } }
It takes two arguments.
1) the array field
2) Value to be appended.
Note: If the array field is not available then a new array field will be created.
Example:
[Link]({},{$push:{skills:"Oracle"}})
$push operator modifiers
$each
Used to append multiple values into an array
Example:
[Link]({},{$push :{skills:{$each:["Oracle","Java","Python"]}}})
$push operator modifiers
$sort
The $sort modifiers orders the items in the array.
This modifier also requires the use of $each modifier.
Example:
[Link]({},{$push
:{skills:{$each:
["silver","orange","whilte"],
$sort:1}}})
$push operator modifiers
$slice
The $slice modifier limit the no. of elements in the array.
This modifier also requires the use of $each modifier.
Example:
[Link]({},{$push
:{skills:{$each:
["violet","red","black"],
$sort:1,$slice:7}}})
$push operator modifiers
$position
The $position modifier is used to locate the position of
the element in the array where the new element should be added.
This modifier also requires the use of $each modifier.
Example:
[Link]({},{$push :{skills:{$each:["violet"]
,$position:0}}})
$pull
$pull : Removes values from an array that matches a specified condition.
Example:
[Link]({},{$pull :{skills:"violet"}})
[Link]({},{$pull :{skills:{$in:["black","blue"]}}})
[Link]({},{$pull :{skills:{color:{$eq:"gray"}}}})
$unwind
$pull : Used to deconstruct an array field from the input document and a
document for each element.
Example: [Link]({$project:{_id:0,ename:1,skills:1}},
{$unwind:"$skills"})
Arithmetic operations.
$add :
Mainly used to add a set of numbers,
Can also be used to add a certain time interval to a
specified time.
The unit of time is Milliseconds
Syntax : { $add: [ <expression1>, <expression2>, … ] }
Example:
[Link]([{$project:{_id:0,ename:1,salary:1,newSalary:{$add:["$
salary",1000]}}}])
[Link]([{$project:{_id:0,ename:1,hiredate:1,confirmationDate
:{$add:["$hiredate",100*24*60*60*1000]}}}])
Arithmetic operations.
$subtract :
calculates the difference between two values; calculates
the interval between two dates; and subtracts the specified
interval from the specified date.
The unit of time is Milliseconds
Syntax : { $subtract: [ <expression1>, <expression2>, … ] }
Example:
[Link]([{$project:{_id:0,ename:1,salary:1,reducedSalary:{$su
btract:["$salary",1000]}}}])
[Link]([{$project:{_id:0,ename:1,hiredate:1,days:{$subtract:[
Arithmetic operations.
$divide :
The $divide operator is mainly used to divide two numbers.
Syntax : { $divide: [ <expression1>, <expression2>, … ] }
Example:
[Link]([{$project:
{_id:0,ename:1,salary:1,eprcentageComm:{
$divide:["$salary",100]}}}])
[Link]([{$project:{_id:0,ename:1,hiredate:1,days:{$divide:[{$
subtract:[new Date(),"$hiredate"]},1000*60*60*24]}}}])
[Link]([{$project:{_id:0,ename:1,hiredate:1,days:{$trunc:{$d
ivide:[{$subtract:[new Date(),"$hiredate"]},1000*60*60*24]}}}}])
Arithmetic operations.
$inc : Increase a value.
[Link]({ _id: 1}, {$inc: {price: 50}})
$mul : multiply the value of the field by a specified number.
[Link]({ }, {$mul: {price: 1.1}})
String operators
$concat.
$concat : Used to concatenate two or more
strings into a single string.
Syntax : { $concat: [ <expression1>,
<expression2>, … ] }
Example: [Link].$aggregate([{$project:{nameAndJob:{$concat:
["$ename","$job"
]}}}])
[Link]([{$project:{_id:0,nameAndJob:{$concat:["$ename","
is working as ","$job"]}}}])
String operators
$indexOfBytes.
searches a string for an occurrence of a substring
and returns the UTF-8 byte index of the first occurrence.
strings into a single string.
{ $indexOfBytes: [ <string expression>, <substring
expression>, <start>,
<end> ] }
Example: [Link]([{$project:{ename:1,eposition:
{$indexOfBytes:["$ena me","e"]}}}])
[Link]([{$project:{ename:1,eposition:{$indexOfBytes:["$ena
me","r",3]}}}])
String operators
$split.
divides a string into an array of substrings based on a delimiter.
The delimiter is removed from the string, and the substrings are added as
elements to the array.
Example: [Link]([{$project:{_id:0,result:{$split:
["$ename","a"]}}}])
String operators
$substr.
Returns a portion of the given string starting at specified index position
and including specified no. of characters.
Syntax: { $substr: [ <string>, <start>, <length> ] }
Example:
[Link]([{$project:{_id:0,result:
{$substr:["$ename",0,3]}}}])
String operators
$strLenCP
$strLenCP : returns the length of the string
Example: [Link]([{$project:{_id:0,ename:1,Length:
{$strLenCP:"$enam e"}}}])
Display the names of the employees who have exactly 5 characters in the
names. [Link]({$expr:{$eq:[{$strLenCP:"$ename"},4]}},
{_id:0,ename:1})
String operators
$toLower and $toUpper
$stoLower : returns the length of the string
$stoUpper : returns the length of the string
Example: [Link]([{$project:{names:
{$toLower:"$ename"}}}])
[Link]([{$project:{names:{$toLower:"$ename"}}}])
Working with Date values
Mongo shell provides various methods to store and return date
either as a string or date object.
Date objects are stored as 64 bit integer in [Link] milliseconds.
Methods for returning date values
1. date() → returns a string or as date object
[Link] Date → returns the current date as a Date object.
The ISODate format : YYYY-MM-DDTHH:mm:[Link]
Date aggregation Operators
$dayOfYear : Returns the day of the year for a date as a
number between 1 and 366 (leap year).
[Link]([{$project:{_id:0,dayjoined:{$dayOfYear:"$hiredate
"}}}])
$dayOfMonth : Returns the day of the month for a date as a number
between 1 and 31.
[Link]([{$project:{_id:0,month:{$dayOfMonth:"$hiredate
"}}}])
Date aggregation Operators
$dayOfWeek : Returns the day of the week as a number between 1
and 7 (leap year).
[Link]([{$project:{_id:0,dayofweek:{$dayOfWeek:"$hired
ate"}}}])
Other operators
$year, $month,$week,$hour,$minute, $second
Date aggregation Operators
$dateToString : converts a date object to a string value according the
user specified format.
[Link]([{$project:{_id:0,hiredate:1,Dateformat:{$dateToSt
ring:{format:"%d %m %Y",date:"$hiredate"}}}}])
Formats
%Y - year in 4 digits %H - hours in 24 hrs format
%m - month in 2 digits %M - Minutes %S - seconds
%d - Day of the month %j - day of the year
$lookup
$lookup makes it possible to join data from an input collection (the
collection you’re running the query on) and a lookup collection (the
collection you want data from), as long as both collections are on the
same database.
Output documents from the lookup collection are added as
embedded documents in the input collection.
[Link]([{$lookup:{from:"dept",localField:"deptno",foreign
Field:"deptno", as : "empdept"}},{$project:
{_id:0,ename:1,empdept:1}}]).pretty()
MongoDB with Python
▪ Python has a native library for MongoDB called
Pymongo which enables to connect with MongoDB.
▪ Provides a rich set of tools that can be used to communicate
with a MongoDB server.
▪ It provides functionality to query, retrieve results, write and delete
data, and run database commands.
▪ Each module within PyMongo is responsible for a set of operations
on the database.
Problem Statement
A small banking system provides an application to customers. The
system should store all the information about the customers and the
accounts. The details of the transactions also to be maintained.
The data is stored in three collections
a) Customers
b) Accounts
c) Transactions
Installing PyMongo
▪ Make sure that python software is installed on your system.
▪ Next install pymongo in the python environment.
c:> pip install pymongo
Establishing a connection
First import pymongo and pprint libraries
>> import pymongo
>> import pprint
Then create a mongoClient class.
>> from pymongo import MongoClient
Next connect to the server using hostname and port
>> client = MongoClient('localhost',27017)
Or
client = MongoClient("mongodb://localhost:27017/")
List the databases in the server
>>import pymongo
>>import pprint
>>from pymongo import MongoClient
>>client = MongoClient("mongodb://[Link]:27017/")
>>print(client.list_database_names())
Creating a Database
Create a new database or switch to existing one.
>> variablename = client[‘database_name’]
Or
>> variablename = client.database_name
Example:
>> mydb = client[‘bank’]
Or
mydb=[Link]
Creating a collection
To create a collection
>> variablename = client[‘database_name’]
Or
>> variablename = client.database_name
Example:
>> mycol=mydb['customers']
Check the list of collections
print(mydb.list_collection_names())
Inserting documents
First store the object in a variable
doc1={"cid":54321,"cname":"Prakash","city":"cbe"}
Use insert_one method to insert the document into the collection
mycol.insert_one(doc1)
Insert multiple values
Create the list of documents and then insert
cust_list=[
{"cid":87345,"cname":"Smith","city":"CHE"},
{"cid":65453,"cname":"Allen","city":"BLR"},
{"cid":87345,"cname":"Jones","city":"HYD"}
]
mycol.insert_many(cust_list)
Reading documents
Use the find_one() method of pymongo to read the first
document from the collection.
print(mycol.find_one())
Reading multiple records in a collection.
for result in [Link]():
print(result)
Method 2:
Cursor = [Link]()
For record in cursor
print(record)
Datetime and timezone
PyMongo uses [Link] objects for representing dates and times in MongoDB documents.
MongoDB assumes that dates and times are in UTC
Avoid using
[Link]() Example:
import pymongo
from pymongo import MongoClient
import datetime
client = MongoClient("mongodb://[Link]:27017/")
mydb = [Link]
mycol=mydb['trans']
output=mycol.insert_one({"trans_date":[Link]()})
x=mycol.find_one()
print(x) print(mycol.find_one({},
{"_id":0}))
Interacting with HTML using
FLASK
What is flask?
Flask is a web framework that provides libraries to build lightweight web
applications in python
It allows users to fetch data from the server and also store data in the
server.
It has features like url routing, temple engine etc.
It’s dependencies are
▪ Werkzueg a WSGI utility (Web Server
Gateway Interface)
▪ Jinja2 a template engine which holds static
Interacting with HTML using
FLASK
Get started
1. First create a folder for the project. Ex: mybank and add
two subfolders static and templates inside it.
2. Create a new file in the folder and name it with .py
extension
3. Next import all the required libraries into the application
>>from flask import
Flask,render_template,request,redirect,url_for
>>from pymongo import MongoClient
Interacting with HTML using
FLASK
Next Create the flask application
pip install flask
create the flask backend which will get user input from HTML form
Syntax: url_for{{(‘function_name’)}}
Interacting with HTML using
FLASK
First create the form using HTML.
<form action="{{ url_for("emp")}}" method="post">
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="fname" placeholder="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lname" placeholder="lastname">
<button type="submit">Login</button>
url_for is an Flask way of creating dynamic URLs where the first argument refers to the function
of that specific route in flask.
Syntax: url_for{{(‘function_name’)}}