SlideShare a Scribd company logo
DB
MG
NoSQL databases
Introduction to MongoDB
DB
MG
MongoDB: Introduction
The leader in the NoSQL Document-based
databases
Full of features, beyond NoSQL
High performance
High availability
Native scalability
High flexibility
Open source
DB
MG
Terminology – Approximate mapping
Relational database MongoDB
Table Collection
Record Document
Column Field
DB
MG
MongoDB: Document Data Design
High-level, business-ready representation of the data
Records are stored into Documents
• field-value pairs
• similar to JSON objects
• may be nested
DB
MG
MongoDB: Document Data Design
High-level, business-ready representation of the data
Flexible and rich syntax, adapting to most use cases
Mapping into developer-language objects
year, month, day, timestamp,
lists, sub-documents, etc.
DB
MG
MongoDB: Main features
Rich query language
Documents can be created, read, updated and
deleted.
The SQL language is not supported
APIs available for many programming languages
JavaScript, PHP, Python, Java, C#, ..
6
DB
MG
MongoDB
Querying data using operators
DB
MG
MongoDB: query language
MySQL clause MongoDB operator
SELECT find()
SELECT *
FROM people
db.people.find()
Most of the operations available in SQL language
can be expressend in MongoDB language
DB
MG
MongoDB: Read data from documents
Select documents
db.<collection name>.find( {<conditions>},
{<fields of interest>} );
E.g.,
db.people.find();
Returns all documents contained in the people
collection
DB
MG
MongoDB: Read data from documents
Select documents
db.<collection name>.find( {<conditions>},
{<fields of interest>} );
Select the documents satisfying the specified
conditions and specifically only the fields
specified in fields of interest
<conditions> are optional
conditions take a document with the form:
{field1 : <value>, field2 : <value> ... }
Conditions may specify a value or a regular
expression
DB
MG
MongoDB: Read data from documents
Select documents
db.<collection name>.find( {<conditions>},
{<fields of interest>} );
Select the documents satisfying the specified
conditions and specifically only the fields
specified in fields of interest
<fields of interest> are optional
projections take a document with the form:
{field1 : <value>, field2 : <value> ... }
1/true to include the field, 0/false to exclude the
field
DB
MG
MongoDB: Read data from documents
E.g.,
db.people.find().pretty();
No conditions and no fields of interest
Returns all documents contained in the people
collection
pretty()displays the results in an easy-to-read
format
db.people.find({age:55})
One condition on the value of age
Returns all documents having age equal to 55
DB
MG
MongoDB: Read data from documents
db.people.find({ }, { user_id: 1, status: 1 })
No conditions, but returns a specific set of fields
of interest
Returns only user_id and status of all documents contained in the
people collection
Default of fields is false, except for _id
db.people.find({ status: "A", age: 55})
status = “A” and age = 55
Returns all documents having status = “A” and age = 55
DB
MG
MongoDB: find() operator
MySQL clause MongoDB operator
SELECT find()
SELECT id,
user_id,
status
FROM people
db.people.find(
{ },
{ user_id: 1,
status: 1
}
)
DB
MG
MongoDB: find() operator
MySQL clause MongoDB operator
SELECT find()
SELECT id,
user_id,
status
FROM people
db.people.find(
{ },
{ user_id: 1,
status: 1
}
)
Where Condition
Select fields
DB
MG
MongoDB: find() operator
MySQL clause MongoDB operator
SELECT find()
WHERE find({<WHERE CONDITIONS>})
SELECT *
FROM people
WHERE status = "A"
db.people.find(
{ status: "A" }
)
Where Condition
DB
MG
MongoDB: find() operator
SELECT user_id, status
FROM people
WHERE status = "A"
db.people.find(
{ status: "A" },
{ user_id: 1,
status: 1,
_id: 0
}
)
Where Condition
Selection fields
MySQL clause MongoDB operator
SELECT find()
WHERE find({<WHERE CONDITIONS>})
By default, the _id field is shown.
To remove it from visualization use: _id: 0
DB
MG
MongoDB: find() operator
MySQL clause MongoDB operator
SELECT find()
WHERE find({<WHERE CONDITIONS>})
db.people.find(
{"address.city":“Rome" }
)
nested document
{ _id: "A",
address: {
street: “Via Torino”,
number: “123/B”,
city: “Rome”,
code: “00184”
}
}
DB
MG
MongoDB: Read data from documents
db.people.find({ age: { $gt: 25, $lte: 50 } })
Age greater than 25 and less than or equal to 50
Returns all documents having age > 25 and age <= 50
db.people.find({$or:[{status: "A"},{age: 55}]})
Status = “A” or age = 55
Returns all documents having status=“A” or age=55
db.people.find({ status: {$in:["A", "B"]}})
Status = “A” or status = B
Returns all documents where the status field value is either
“A” or “B”
DB
MG
MongoDB: Read data from documents
Select a single document
db.<collection name>.findOne(
{<conditions>}, {<fields of interest>} );
Select one document that satisfies the specified
query criteria.
If multiple documents satisfy the query, it returns
the first one according to the natural order which
reflects the order of documents on the disk.
DB
MG
MongoDB: (no) joins
There are other operators for selecting data from
MongoDB collections
However, no join operator exists (but $lookup)
You must write a program that
Selects the documents of the first collection you are
interested in
Iterates over the documents returned by the first
step, by using the loop statement provided by the
programming language you are using
Executes one query for each of them to retrieve the
corresponding document(s) in the other collection
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup
DB
MG
MongoDB: (no) joins
(no) joins
Relations among documents/records are provided by
Object(ID) reference, with no native join
DBRef, across collections and databases
https://docs.mongodb.com/manual/reference/database-references/
DB
MG
MongoDB: comparison operators
In SQL language, comparison operators are
essential to express conditions on data.
In Mongo query language they are available with
a different syntax.
MySQL MongoDB Description
> $gt greater than
>= $gte greater equal then
< $lt less than
<= $lte less equal then
= $eq equal to
!= $neq not equal to
DB
MG
MongoDB: Comparison query operators
Name Description
$eq or : Matches values that are equal to a specified value
$gt Matches values that are greater than a specified value
$gte Matches values that are greater than or equal to a
specified value
$in Matches any of the values specified in an array
$lt Matches values that are less than a specified value
$lte Matches values that are less than or equal to a specified
value
$ne Matches all values that are not equal to a specified value
$nin Matches none of the values specified in an array
DB
MG
MongoDB: comparison operators (>)
MySQL MongoDB Description
> $gt greater than
SELECT *
FROM people
WHERE age > 25
db.people.find(
{ age: { $gt: 25 } }
)
DB
MG
MongoDB: comparison operators (>=)
SELECT *
FROM people
WHERE age >= 25
db.people.find(
{ age: { $gte: 25 } }
)
MySQL MongoDB Description
> $gt greater than
>= $gte greater equal then
DB
MG
MongoDB: comparison operators (<)
SELECT *
FROM people
WHERE age < 25
db.people.find(
{ age: { $lt: 25 } }
)
MySQL MongoDB Description
> $gt greater than
>= $gte greater equal then
< $lt less than
DB
MG
MongoDB: comparison operators (<=)
SELECT *
FROM people
WHERE age <= 25
db.people.find(
{ age: { $lte: 25 } }
)
MySQL MongoDB Description
> $gt greater than
>= $gte greater equal then
< $lt less than
<= $lte less equal then
DB
MG
MongoDB: comparison operators (=)
MySQL MongoDB Description
> $gt greater than
>= $gte greater equal then
< $lt less than
<= $lte less equal then
= $eq equal to
The $eq expression is
equivalent to
{ field: <value> }.
SELECT *
FROM people
WHERE age = 25
db.people.find(
{ age: { $eq: 25 } }
)
DB
MG
MongoDB: comparison operators (!=)
MySQL MongoDB Description
> $gt greater than
>= $gte greater equal then
< $lt less than
<= $lte less equal then
= $eq equal to
!= $neq Not equal to
SELECT *
FROM people
WHERE age != 25
db.people.find(
{ age: { $neq: 25 } }
)
DB
MG
MongoDB: conditional operators
To specify multiple conditions, conditional
operators are used
MongoDB offers the same functionalities of
MySQL with a different syntax.
MySQL MongoDB Description
AND , Both verified
OR $or At least one verified
DB
MG
MongoDB: conditional operators (AND)
MySQL MongoDB Description
AND , Both verified
SELECT *
FROM people
WHERE status = "A"
AND age = 50
db.people.find(
{ status: "A",
age: 50 }
)
DB
MG
MongoDB: conditional operators (OR)
MySQL MongoDB Description
AND , Both verified
OR $or At least one verified
SELECT *
FROM people
WHERE status = "A"
OR age = 50
db.people.find(
{ $or:
[ { status: "A" } ,
{ age: 50 }
]
}
)
DB
MG
MongoDB: Cursor
db.collection.find()gives back a cursor. It
can be used to iterate over the result or as input
for next operations.
E.g.,
cursor.sort()
cursor.count()
cursor.forEach() //shell method
cursor.limit()
cursor.max()
cursor.min()
cursor.pretty()
DB
MG
MongoDB: Cursor
Cursor examples:
db.people.find({ status: "A"}).count()
Select documents with status=“A” and count them.
db.people.find({ status: "A"}).forEach(
function(myDoc) { print( "user: ”+myDoc.name );
})
forEach applies a JavaScript function to apply to
each document from the cursor.
Select documents with status=“A” and print the
document name.
DB
MG
MongoDB: sorting data
Sort is a cursor method
Sort documents
sort( {<list of field:value pairs>} );
field specifies which filed is used to sort the
returned documents
value = -1 descending order
Value = 1 ascending order
Multiple field: value pairs can be specified
Documents are sort based on the first field
In case of ties, the second specified field is
considered
DB
MG
MongoDB: sorting data
E.g.,
db.people.find({ status: "A"}).sort({age:1})
Select documents with status=“A” and sort them
in ascending order based on the age value
Returns all documents having status=“A”. The result
is sorted in ascending age order
DB
MG
SELECT *
FROM people
WHERE status = "A"
ORDER BY user_id ASC
db.people.find(
{ status: "A" }
).sort( { user_id: 1 } )
MongoDB: sorting data
MySQL clause MongoDB operator
ORDER BY sort()
Sorting data with respect to a given field in
MongoDB: sort() operator
DB
MG
SELECT *
FROM people
WHERE status = "A"
ORDER BY user_id ASC
db.people.find(
{ status: "A" }
).sort( { user_id: 1 } )
MongoDB: sorting data
MySQL clause MongoDB operator
ORDER BY sort()
Sorting data with respect to a given field in
MongoDB: sort() operator
SELECT *
FROM people
WHERE status = "A"
ORDER BY user_id DESC
db.people.find(
{ status: "A" }
).sort( { user_id: -1 } )
DB
MG
MongoDB: counting
SELECT COUNT(*)
FROM people
db.people.count()
or
db.people.find().count()
MySQL clause MongoDB operator
COUNT count()or find().count()
DB
MG
MongoDB: counting
SELECT COUNT(*)
FROM people
WHERE age > 30
db.people.count(
{ age: { $gt: 30 } }
)
MySQL clause MongoDB operator
COUNT count()or find().count()
Similar to the find() operator, count() can embed
conditional statements.
DB
MG
MongoDB
Introduction to data aggregation
DB
MG
Aggregation in MongoDB
Aggregation operations process data records and
return computed results.
Documents enter a multi-stage pipeline that
transforms the documents into an aggregated
result.
DB
MG
MongoDB: Aggregation Framework
SQL MongoDB
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
//LIMIT $limit
SUM $sum
COUNT $sum
DB
MG
MongoDB: Aggregation
Aggregate functions can be applied to collections
to group documents
db.collection.aggregate({<set of stages>})
Common stages: $match, $group ..
The aggregate function allows applying
aggregating functions (e.g. sum, average, ..)
It can be combined with an initial definition of
groups based on the grouping fields
DB
MG
MongoDB: Aggregation
db.people.aggregate( [
{ $group: { _id: null,
mytotal: { $sum: "$age" },
mycount: { $sum: 1 }
}
}
] )
Considers all documents of people and
sum the values of their age
sum a set of ones (one for each document)
The returned value is associated with a field
called “mytotal” and a field “mycount”
DB
MG
MongoDB: Aggregation
db.people.aggregate( [
{ $group: { _id: null,
myaverage: { $avg: "$age" },
mytotal: { $sum: "$age" }
}
}
] )
Considers all documents of people and computes
sum of age
average of age
DB
MG
MongoDB: Aggregation
db.people.aggregate( [
{ $match: {status: "A"} } ,
{ $group: { _id: null,
count: { $sum: 1 }
}
}
] )
Counts the number of documents in people with
status equal to “A”
Where conditions
DB
MG
MongoDB: Aggregation
db.people.aggregate( [
{ $group: { _id: "$status",
count: { $sum: 1 }
}
}
] )
Creates one group of documents for each value of
status and counts the number of documents per
group
Returns one value for each group containing the
value of the grouping field and an integer
representing the number of documents
DB
MG
MongoDB: Aggregation
db.people.aggregate( [
{ $group: { _id: "$status",
count: { $sum: 1 }
}
},
{ $match: { count: { $gte: 3 } } }
] )
Creates one group of documents for each value
of status and counts the number of documents
per group. Returns only the groups with at least
3 documents
DB
MG
MongoDB: Aggregation
db.people.aggregate( [
{ $group: { _id: "$status",
count: { $sum: 1 }
}
},
{ $match: { count: { $gte: 3 } } }
] )
Creates one group of documents for each value
of status and counts the number of documents
per group. Returns only the groups with at least
3 documents
Having condition
DB
MG
MongoDB: Aggregation Framework
SQL MongoDB
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM $sum
COUNT $sum
DB
MG
SELECT status,
AVG(age) AS total
FROM people
GROUP BY status
db.orders.aggregate( [
{
$group: {
_id: "$status",
total: { $avg: "$age" }
}
}
] )
Aggregation in MongoDB: Group By
MySQL clause MongoDB operator
GROUP BY aggregate($group)
DB
MG
SELECT status,
SUM(age) AS total
FROM people
GROUP BY status
db.orders.aggregate( [
{
$group: {
_id: "$status",
total: { $sum: "$age" }
}
}
] )
Aggregation in MongoDB: Group By
MySQL clause MongoDB operator
GROUP BY aggregate($group)
Group field
DB
MG
SELECT status,
SUM(age) AS total
FROM people
GROUP BY status
db.orders.aggregate( [
{
$group: {
_id: "$status",
total: { $sum: "$age" }
}
}
] )
Aggregation in MongoDB: Group By
MySQL clause MongoDB operator
GROUP BY aggregate($group)
Group field
Aggregation function
DB
MG
SELECT status,
SUM(age) AS total
FROM people
GROUP BY status
HAVING total > 1000
db.orders.aggregate( [
{
$group: {
_id: "$status",
total: { $sum: "$age" }
}
},
{ $match: { total: { $gt: 1000 } } }
] )
Aggregation in MongoDB: Group By
MySQL clause MongoDB operator
HAVING aggregate($group, $match)
DB
MG
SELECT status,
SUM(age) AS total
FROM people
GROUP BY status
HAVING total > 1000
db.orders.aggregate( [
{
$group: {
_id: "$status",
total: { $sum: "$age" }
}
},
{ $match: { total: { $gt: 1000 } } }
] )
Aggregation in MongoDB: Group By
MySQL clause MongoDB operator
HAVING aggregate($group, $match)
Group stage: Specify
the aggregation field
and the aggregation
function
DB
MG
SELECT status,
SUM(age) AS total
FROM people
GROUP BY status
HAVING total > 1000
db.orders.aggregate( [
{
$group: {
_id: "$status",
total: { $sum: "$age" }
}
},
{ $match: { total: { $gt: 1000 } } }
] )
Aggregation in MongoDB: Group By
MySQL clause MongoDB operator
HAVING aggregate($group, $match)
Group stage: Specify
the aggregation field
and the aggregation
function
Match Stage: specify
the condition as in
HAVING
DB
MG
Aggregation in MongoDB
DB
MG
MongoDB Compass
GUI for Mongo DB
DB
MG
MongoDB Compass
Visually explore data.
Available on Linux, Mac, or Windows.
MongoDB Compass analyzes documents and
displays rich structures within collections.
Visualize, understand, and work with your
geospatial data.
DB
MG
MongoDB Compass
Connect to local or remote instances of MongoDB.
DB
MG
MongoDB Compass
Get an overview of the data in list or table format.
DB
MG
MongoDB Compass
Analyze the documents and their fields.
Native support for geospatial coordinates.
DB
MG
MongoDB Compass
Visually build the query conditioning on
analyzed fields.
DB
MG
MongoDB Compass
Autocomplete enabled by default.
Construct the query step by step.
DB
MG
MongoDB Compass
Analyze query performance and get hints to speed it up.
DB
MG
MongoDB Compass
Specify contraints to validate data
Find unconsistent documents.
DB
MG
MongoDB Compass: Aggregation
Build a pipeline
consisting of multiple
aggregation stages.
Define the filter and
aggregation attributes
for each operator.
DB
MG
MongoDB Compass: Aggregation stages
DB
MG
MongoDB Compass: Aggregation stages
The _id corresponds to
the GROUP BY
parameter in SQL
Other fields contain the
attributes required for
each group.
One group for each “vendor”.
DB
MG
MongoDB Compass: Pipelines
1st stage: grouping by vendor
2nd stage: condition over fields created in
the previous stage (avg_fuel, total).
DB
MG
MongoDB
Indexing
DB
MG
MongoDB: Indexes
Indexes are data structures that store a small
portion of the collection’s data set in a form easy
to traverse.
They store ordered values of a specific field, or
set of fields, in order to efficiently support
equality matches, range-based queries and
sorting operations.
DB
MG
MongoDB: Indexes
MongoDB provides different data-type indexes
Single field indexes
Compound field indexes
Multikey indexes
Geospatial indexes
Text indexes
Hashed indexes
DB
MG
MongoDB: Create new indexes
Creating an index
db.collection.createIndex(<index keys>, <options>)
Before v. 3.0 use db.collection.ensureIndex()
Options include: name, unique (whether to accept
or not insertion of documents with duplicate index
keys), background, dropDups, ..
DB
MG
MongoDB: Indexes
Single field indexes
Support user-defined ascending/descending
indexes on a single field of a document
E.g.,
db.orders.createIndex( {orderDate: 1} )
Compound field indexes
Support user-defined indexes on a set of fields
E.g.,
db.orders.createIndex( {orderDate: 1,
zipcode: -1} )
DB
MG
MongoDB: Indexes
MongoDB supports efficient queries of geospatial
data
Geospatial data are stored as:
GeoJSON objects: embedded document { <type>,
<coordinate> }
E.g., location: {type: "Point", coordinates: [-
73.856, 40.848]}
Legacy coordinate pairs: array or embedded document
point: [-73.856, 40.848]
DB
MG
MongoDB: Indexes
Geospatial indexes
Two type of geospatial indexes are provided: 2d
and 2dsphere
A 2dsphere index supports queries that
calculate geometries on an earth-like sphere
Use a 2d index for data stored as points on a
two-dimensional plane.
E.g.,
db.places.createIndex( {location: “2dsphere”} )
Geospatial query operators
$geoIntersects, $geoWithin, $near, $nearSphere
DB
MG
MongoDB: Indexes
{
<location field>: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [ <longitude> , <latitude> ]
},
$maxDistance: <distance in meters>,
$minDistance: <distance in meters>
}
}
}
$near syntax:
DB
MG
MongoDB: Indexes
E.g.,
db.places.createIndex( {location: “2dsphere”} )
Geospatial query operators
$geoIntersects, $geoWithin, $near, $nearSphere
Geopatial aggregation stage
$near
DB
MG
MongoDB: Indexes
E.g.,
db.places.find({location:
{$near:
{$geometry: {
type: "Point",
coordinates: [ -73.96, 40.78 ] },
$maxDistance: 5000}
}})
Find all the places within 5000 meters from the
specified GeoJSON point, sorted in order from
nearest to furthest
DB
MG
MongoDB: Indexes
Text indexes
Support efficient searching for string content in a
collection
Text indexes store only root words (no language-
specific stop words or stem)
E.g.,
db.reviews.createIndex( {comment: “text”} )
Wildcard ($**) allows MongoDB to index every
field that contains string data
E.g.,
db.reviews.createIndex( {“$**”: “text”} )

More Related Content

PPTX
UNIT-1 MongoDB.pptx
PPTX
MongoDB Knowledge share
PPTX
Mongo DB Presentation
PPS
MongoDB crud
PPT
Mongo db basics
PDF
Mongo db
PPTX
PPTX
Introduction to MongoDB
UNIT-1 MongoDB.pptx
MongoDB Knowledge share
Mongo DB Presentation
MongoDB crud
Mongo db basics
Mongo db
Introduction to MongoDB

Similar to Slide perkenalan dengan dasar MongoDB-query (20)

PDF
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
PPTX
Mongo db basic installation
PPTX
PPTX
Mongo Nosql CRUD Operations
PDF
Mongo db basics
PPTX
Introduction to MongoDB – A NoSQL Database
KEY
MongoDB at GUL
PPTX
Mongo db queries
PDF
PDF
Full metal mongo
PPTX
MongoDB_ppt.pptx
KEY
Introduction to MongoDB
PPTX
mongodb introduction11111111111111111111
PPTX
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
PDF
full stack modul 5, mongodb,webpack,front-end,back-end
PPTX
No SQL DB lecture showing structure and syntax
PPTX
Introduction To MongoDB
KEY
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
PDF
MongoD Essentials
PPTX
introtomongodb
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
Mongo db basic installation
Mongo Nosql CRUD Operations
Mongo db basics
Introduction to MongoDB – A NoSQL Database
MongoDB at GUL
Mongo db queries
Full metal mongo
MongoDB_ppt.pptx
Introduction to MongoDB
mongodb introduction11111111111111111111
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
full stack modul 5, mongodb,webpack,front-end,back-end
No SQL DB lecture showing structure and syntax
Introduction To MongoDB
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
MongoD Essentials
introtomongodb
Ad

Recently uploaded (20)

PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
1_Introduction to advance data techniques.pptx
PDF
Foundation of Data Science unit number two notes
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Introduction to machine learning and Linear Models
PPTX
IB Computer Science - Internal Assessment.pptx
PPT
Quality review (1)_presentation of this 21
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Lecture1 pattern recognition............
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Business Acumen Training GuidePresentation.pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
1_Introduction to advance data techniques.pptx
Foundation of Data Science unit number two notes
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Data_Analytics_and_PowerBI_Presentation.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Introduction to machine learning and Linear Models
IB Computer Science - Internal Assessment.pptx
Quality review (1)_presentation of this 21
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
STUDY DESIGN details- Lt Col Maksud (21).pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Lecture1 pattern recognition............
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Qualitative Qantitative and Mixed Methods.pptx
Miokarditis (Inflamasi pada Otot Jantung)
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Ad

Slide perkenalan dengan dasar MongoDB-query

  • 2. DB MG MongoDB: Introduction The leader in the NoSQL Document-based databases Full of features, beyond NoSQL High performance High availability Native scalability High flexibility Open source
  • 3. DB MG Terminology – Approximate mapping Relational database MongoDB Table Collection Record Document Column Field
  • 4. DB MG MongoDB: Document Data Design High-level, business-ready representation of the data Records are stored into Documents • field-value pairs • similar to JSON objects • may be nested
  • 5. DB MG MongoDB: Document Data Design High-level, business-ready representation of the data Flexible and rich syntax, adapting to most use cases Mapping into developer-language objects year, month, day, timestamp, lists, sub-documents, etc.
  • 6. DB MG MongoDB: Main features Rich query language Documents can be created, read, updated and deleted. The SQL language is not supported APIs available for many programming languages JavaScript, PHP, Python, Java, C#, .. 6
  • 8. DB MG MongoDB: query language MySQL clause MongoDB operator SELECT find() SELECT * FROM people db.people.find() Most of the operations available in SQL language can be expressend in MongoDB language
  • 9. DB MG MongoDB: Read data from documents Select documents db.<collection name>.find( {<conditions>}, {<fields of interest>} ); E.g., db.people.find(); Returns all documents contained in the people collection
  • 10. DB MG MongoDB: Read data from documents Select documents db.<collection name>.find( {<conditions>}, {<fields of interest>} ); Select the documents satisfying the specified conditions and specifically only the fields specified in fields of interest <conditions> are optional conditions take a document with the form: {field1 : <value>, field2 : <value> ... } Conditions may specify a value or a regular expression
  • 11. DB MG MongoDB: Read data from documents Select documents db.<collection name>.find( {<conditions>}, {<fields of interest>} ); Select the documents satisfying the specified conditions and specifically only the fields specified in fields of interest <fields of interest> are optional projections take a document with the form: {field1 : <value>, field2 : <value> ... } 1/true to include the field, 0/false to exclude the field
  • 12. DB MG MongoDB: Read data from documents E.g., db.people.find().pretty(); No conditions and no fields of interest Returns all documents contained in the people collection pretty()displays the results in an easy-to-read format db.people.find({age:55}) One condition on the value of age Returns all documents having age equal to 55
  • 13. DB MG MongoDB: Read data from documents db.people.find({ }, { user_id: 1, status: 1 }) No conditions, but returns a specific set of fields of interest Returns only user_id and status of all documents contained in the people collection Default of fields is false, except for _id db.people.find({ status: "A", age: 55}) status = “A” and age = 55 Returns all documents having status = “A” and age = 55
  • 14. DB MG MongoDB: find() operator MySQL clause MongoDB operator SELECT find() SELECT id, user_id, status FROM people db.people.find( { }, { user_id: 1, status: 1 } )
  • 15. DB MG MongoDB: find() operator MySQL clause MongoDB operator SELECT find() SELECT id, user_id, status FROM people db.people.find( { }, { user_id: 1, status: 1 } ) Where Condition Select fields
  • 16. DB MG MongoDB: find() operator MySQL clause MongoDB operator SELECT find() WHERE find({<WHERE CONDITIONS>}) SELECT * FROM people WHERE status = "A" db.people.find( { status: "A" } ) Where Condition
  • 17. DB MG MongoDB: find() operator SELECT user_id, status FROM people WHERE status = "A" db.people.find( { status: "A" }, { user_id: 1, status: 1, _id: 0 } ) Where Condition Selection fields MySQL clause MongoDB operator SELECT find() WHERE find({<WHERE CONDITIONS>}) By default, the _id field is shown. To remove it from visualization use: _id: 0
  • 18. DB MG MongoDB: find() operator MySQL clause MongoDB operator SELECT find() WHERE find({<WHERE CONDITIONS>}) db.people.find( {"address.city":“Rome" } ) nested document { _id: "A", address: { street: “Via Torino”, number: “123/B”, city: “Rome”, code: “00184” } }
  • 19. DB MG MongoDB: Read data from documents db.people.find({ age: { $gt: 25, $lte: 50 } }) Age greater than 25 and less than or equal to 50 Returns all documents having age > 25 and age <= 50 db.people.find({$or:[{status: "A"},{age: 55}]}) Status = “A” or age = 55 Returns all documents having status=“A” or age=55 db.people.find({ status: {$in:["A", "B"]}}) Status = “A” or status = B Returns all documents where the status field value is either “A” or “B”
  • 20. DB MG MongoDB: Read data from documents Select a single document db.<collection name>.findOne( {<conditions>}, {<fields of interest>} ); Select one document that satisfies the specified query criteria. If multiple documents satisfy the query, it returns the first one according to the natural order which reflects the order of documents on the disk.
  • 21. DB MG MongoDB: (no) joins There are other operators for selecting data from MongoDB collections However, no join operator exists (but $lookup) You must write a program that Selects the documents of the first collection you are interested in Iterates over the documents returned by the first step, by using the loop statement provided by the programming language you are using Executes one query for each of them to retrieve the corresponding document(s) in the other collection https://docs.mongodb.com/manual/reference/operator/aggregation/lookup
  • 22. DB MG MongoDB: (no) joins (no) joins Relations among documents/records are provided by Object(ID) reference, with no native join DBRef, across collections and databases https://docs.mongodb.com/manual/reference/database-references/
  • 23. DB MG MongoDB: comparison operators In SQL language, comparison operators are essential to express conditions on data. In Mongo query language they are available with a different syntax. MySQL MongoDB Description > $gt greater than >= $gte greater equal then < $lt less than <= $lte less equal then = $eq equal to != $neq not equal to
  • 24. DB MG MongoDB: Comparison query operators Name Description $eq or : Matches values that are equal to a specified value $gt Matches values that are greater than a specified value $gte Matches values that are greater than or equal to a specified value $in Matches any of the values specified in an array $lt Matches values that are less than a specified value $lte Matches values that are less than or equal to a specified value $ne Matches all values that are not equal to a specified value $nin Matches none of the values specified in an array
  • 25. DB MG MongoDB: comparison operators (>) MySQL MongoDB Description > $gt greater than SELECT * FROM people WHERE age > 25 db.people.find( { age: { $gt: 25 } } )
  • 26. DB MG MongoDB: comparison operators (>=) SELECT * FROM people WHERE age >= 25 db.people.find( { age: { $gte: 25 } } ) MySQL MongoDB Description > $gt greater than >= $gte greater equal then
  • 27. DB MG MongoDB: comparison operators (<) SELECT * FROM people WHERE age < 25 db.people.find( { age: { $lt: 25 } } ) MySQL MongoDB Description > $gt greater than >= $gte greater equal then < $lt less than
  • 28. DB MG MongoDB: comparison operators (<=) SELECT * FROM people WHERE age <= 25 db.people.find( { age: { $lte: 25 } } ) MySQL MongoDB Description > $gt greater than >= $gte greater equal then < $lt less than <= $lte less equal then
  • 29. DB MG MongoDB: comparison operators (=) MySQL MongoDB Description > $gt greater than >= $gte greater equal then < $lt less than <= $lte less equal then = $eq equal to The $eq expression is equivalent to { field: <value> }. SELECT * FROM people WHERE age = 25 db.people.find( { age: { $eq: 25 } } )
  • 30. DB MG MongoDB: comparison operators (!=) MySQL MongoDB Description > $gt greater than >= $gte greater equal then < $lt less than <= $lte less equal then = $eq equal to != $neq Not equal to SELECT * FROM people WHERE age != 25 db.people.find( { age: { $neq: 25 } } )
  • 31. DB MG MongoDB: conditional operators To specify multiple conditions, conditional operators are used MongoDB offers the same functionalities of MySQL with a different syntax. MySQL MongoDB Description AND , Both verified OR $or At least one verified
  • 32. DB MG MongoDB: conditional operators (AND) MySQL MongoDB Description AND , Both verified SELECT * FROM people WHERE status = "A" AND age = 50 db.people.find( { status: "A", age: 50 } )
  • 33. DB MG MongoDB: conditional operators (OR) MySQL MongoDB Description AND , Both verified OR $or At least one verified SELECT * FROM people WHERE status = "A" OR age = 50 db.people.find( { $or: [ { status: "A" } , { age: 50 } ] } )
  • 34. DB MG MongoDB: Cursor db.collection.find()gives back a cursor. It can be used to iterate over the result or as input for next operations. E.g., cursor.sort() cursor.count() cursor.forEach() //shell method cursor.limit() cursor.max() cursor.min() cursor.pretty()
  • 35. DB MG MongoDB: Cursor Cursor examples: db.people.find({ status: "A"}).count() Select documents with status=“A” and count them. db.people.find({ status: "A"}).forEach( function(myDoc) { print( "user: ”+myDoc.name ); }) forEach applies a JavaScript function to apply to each document from the cursor. Select documents with status=“A” and print the document name.
  • 36. DB MG MongoDB: sorting data Sort is a cursor method Sort documents sort( {<list of field:value pairs>} ); field specifies which filed is used to sort the returned documents value = -1 descending order Value = 1 ascending order Multiple field: value pairs can be specified Documents are sort based on the first field In case of ties, the second specified field is considered
  • 37. DB MG MongoDB: sorting data E.g., db.people.find({ status: "A"}).sort({age:1}) Select documents with status=“A” and sort them in ascending order based on the age value Returns all documents having status=“A”. The result is sorted in ascending age order
  • 38. DB MG SELECT * FROM people WHERE status = "A" ORDER BY user_id ASC db.people.find( { status: "A" } ).sort( { user_id: 1 } ) MongoDB: sorting data MySQL clause MongoDB operator ORDER BY sort() Sorting data with respect to a given field in MongoDB: sort() operator
  • 39. DB MG SELECT * FROM people WHERE status = "A" ORDER BY user_id ASC db.people.find( { status: "A" } ).sort( { user_id: 1 } ) MongoDB: sorting data MySQL clause MongoDB operator ORDER BY sort() Sorting data with respect to a given field in MongoDB: sort() operator SELECT * FROM people WHERE status = "A" ORDER BY user_id DESC db.people.find( { status: "A" } ).sort( { user_id: -1 } )
  • 40. DB MG MongoDB: counting SELECT COUNT(*) FROM people db.people.count() or db.people.find().count() MySQL clause MongoDB operator COUNT count()or find().count()
  • 41. DB MG MongoDB: counting SELECT COUNT(*) FROM people WHERE age > 30 db.people.count( { age: { $gt: 30 } } ) MySQL clause MongoDB operator COUNT count()or find().count() Similar to the find() operator, count() can embed conditional statements.
  • 43. DB MG Aggregation in MongoDB Aggregation operations process data records and return computed results. Documents enter a multi-stage pipeline that transforms the documents into an aggregated result.
  • 44. DB MG MongoDB: Aggregation Framework SQL MongoDB WHERE $match GROUP BY $group HAVING $match SELECT $project ORDER BY $sort //LIMIT $limit SUM $sum COUNT $sum
  • 45. DB MG MongoDB: Aggregation Aggregate functions can be applied to collections to group documents db.collection.aggregate({<set of stages>}) Common stages: $match, $group .. The aggregate function allows applying aggregating functions (e.g. sum, average, ..) It can be combined with an initial definition of groups based on the grouping fields
  • 46. DB MG MongoDB: Aggregation db.people.aggregate( [ { $group: { _id: null, mytotal: { $sum: "$age" }, mycount: { $sum: 1 } } } ] ) Considers all documents of people and sum the values of their age sum a set of ones (one for each document) The returned value is associated with a field called “mytotal” and a field “mycount”
  • 47. DB MG MongoDB: Aggregation db.people.aggregate( [ { $group: { _id: null, myaverage: { $avg: "$age" }, mytotal: { $sum: "$age" } } } ] ) Considers all documents of people and computes sum of age average of age
  • 48. DB MG MongoDB: Aggregation db.people.aggregate( [ { $match: {status: "A"} } , { $group: { _id: null, count: { $sum: 1 } } } ] ) Counts the number of documents in people with status equal to “A” Where conditions
  • 49. DB MG MongoDB: Aggregation db.people.aggregate( [ { $group: { _id: "$status", count: { $sum: 1 } } } ] ) Creates one group of documents for each value of status and counts the number of documents per group Returns one value for each group containing the value of the grouping field and an integer representing the number of documents
  • 50. DB MG MongoDB: Aggregation db.people.aggregate( [ { $group: { _id: "$status", count: { $sum: 1 } } }, { $match: { count: { $gte: 3 } } } ] ) Creates one group of documents for each value of status and counts the number of documents per group. Returns only the groups with at least 3 documents
  • 51. DB MG MongoDB: Aggregation db.people.aggregate( [ { $group: { _id: "$status", count: { $sum: 1 } } }, { $match: { count: { $gte: 3 } } } ] ) Creates one group of documents for each value of status and counts the number of documents per group. Returns only the groups with at least 3 documents Having condition
  • 52. DB MG MongoDB: Aggregation Framework SQL MongoDB WHERE $match GROUP BY $group HAVING $match SELECT $project ORDER BY $sort LIMIT $limit SUM $sum COUNT $sum
  • 53. DB MG SELECT status, AVG(age) AS total FROM people GROUP BY status db.orders.aggregate( [ { $group: { _id: "$status", total: { $avg: "$age" } } } ] ) Aggregation in MongoDB: Group By MySQL clause MongoDB operator GROUP BY aggregate($group)
  • 54. DB MG SELECT status, SUM(age) AS total FROM people GROUP BY status db.orders.aggregate( [ { $group: { _id: "$status", total: { $sum: "$age" } } } ] ) Aggregation in MongoDB: Group By MySQL clause MongoDB operator GROUP BY aggregate($group) Group field
  • 55. DB MG SELECT status, SUM(age) AS total FROM people GROUP BY status db.orders.aggregate( [ { $group: { _id: "$status", total: { $sum: "$age" } } } ] ) Aggregation in MongoDB: Group By MySQL clause MongoDB operator GROUP BY aggregate($group) Group field Aggregation function
  • 56. DB MG SELECT status, SUM(age) AS total FROM people GROUP BY status HAVING total > 1000 db.orders.aggregate( [ { $group: { _id: "$status", total: { $sum: "$age" } } }, { $match: { total: { $gt: 1000 } } } ] ) Aggregation in MongoDB: Group By MySQL clause MongoDB operator HAVING aggregate($group, $match)
  • 57. DB MG SELECT status, SUM(age) AS total FROM people GROUP BY status HAVING total > 1000 db.orders.aggregate( [ { $group: { _id: "$status", total: { $sum: "$age" } } }, { $match: { total: { $gt: 1000 } } } ] ) Aggregation in MongoDB: Group By MySQL clause MongoDB operator HAVING aggregate($group, $match) Group stage: Specify the aggregation field and the aggregation function
  • 58. DB MG SELECT status, SUM(age) AS total FROM people GROUP BY status HAVING total > 1000 db.orders.aggregate( [ { $group: { _id: "$status", total: { $sum: "$age" } } }, { $match: { total: { $gt: 1000 } } } ] ) Aggregation in MongoDB: Group By MySQL clause MongoDB operator HAVING aggregate($group, $match) Group stage: Specify the aggregation field and the aggregation function Match Stage: specify the condition as in HAVING
  • 61. DB MG MongoDB Compass Visually explore data. Available on Linux, Mac, or Windows. MongoDB Compass analyzes documents and displays rich structures within collections. Visualize, understand, and work with your geospatial data.
  • 62. DB MG MongoDB Compass Connect to local or remote instances of MongoDB.
  • 63. DB MG MongoDB Compass Get an overview of the data in list or table format.
  • 64. DB MG MongoDB Compass Analyze the documents and their fields. Native support for geospatial coordinates.
  • 65. DB MG MongoDB Compass Visually build the query conditioning on analyzed fields.
  • 66. DB MG MongoDB Compass Autocomplete enabled by default. Construct the query step by step.
  • 67. DB MG MongoDB Compass Analyze query performance and get hints to speed it up.
  • 68. DB MG MongoDB Compass Specify contraints to validate data Find unconsistent documents.
  • 69. DB MG MongoDB Compass: Aggregation Build a pipeline consisting of multiple aggregation stages. Define the filter and aggregation attributes for each operator.
  • 71. DB MG MongoDB Compass: Aggregation stages The _id corresponds to the GROUP BY parameter in SQL Other fields contain the attributes required for each group. One group for each “vendor”.
  • 72. DB MG MongoDB Compass: Pipelines 1st stage: grouping by vendor 2nd stage: condition over fields created in the previous stage (avg_fuel, total).
  • 74. DB MG MongoDB: Indexes Indexes are data structures that store a small portion of the collection’s data set in a form easy to traverse. They store ordered values of a specific field, or set of fields, in order to efficiently support equality matches, range-based queries and sorting operations.
  • 75. DB MG MongoDB: Indexes MongoDB provides different data-type indexes Single field indexes Compound field indexes Multikey indexes Geospatial indexes Text indexes Hashed indexes
  • 76. DB MG MongoDB: Create new indexes Creating an index db.collection.createIndex(<index keys>, <options>) Before v. 3.0 use db.collection.ensureIndex() Options include: name, unique (whether to accept or not insertion of documents with duplicate index keys), background, dropDups, ..
  • 77. DB MG MongoDB: Indexes Single field indexes Support user-defined ascending/descending indexes on a single field of a document E.g., db.orders.createIndex( {orderDate: 1} ) Compound field indexes Support user-defined indexes on a set of fields E.g., db.orders.createIndex( {orderDate: 1, zipcode: -1} )
  • 78. DB MG MongoDB: Indexes MongoDB supports efficient queries of geospatial data Geospatial data are stored as: GeoJSON objects: embedded document { <type>, <coordinate> } E.g., location: {type: "Point", coordinates: [- 73.856, 40.848]} Legacy coordinate pairs: array or embedded document point: [-73.856, 40.848]
  • 79. DB MG MongoDB: Indexes Geospatial indexes Two type of geospatial indexes are provided: 2d and 2dsphere A 2dsphere index supports queries that calculate geometries on an earth-like sphere Use a 2d index for data stored as points on a two-dimensional plane. E.g., db.places.createIndex( {location: “2dsphere”} ) Geospatial query operators $geoIntersects, $geoWithin, $near, $nearSphere
  • 80. DB MG MongoDB: Indexes { <location field>: { $near: { $geometry: { type: "Point" , coordinates: [ <longitude> , <latitude> ] }, $maxDistance: <distance in meters>, $minDistance: <distance in meters> } } } $near syntax:
  • 81. DB MG MongoDB: Indexes E.g., db.places.createIndex( {location: “2dsphere”} ) Geospatial query operators $geoIntersects, $geoWithin, $near, $nearSphere Geopatial aggregation stage $near
  • 82. DB MG MongoDB: Indexes E.g., db.places.find({location: {$near: {$geometry: { type: "Point", coordinates: [ -73.96, 40.78 ] }, $maxDistance: 5000} }}) Find all the places within 5000 meters from the specified GeoJSON point, sorted in order from nearest to furthest
  • 83. DB MG MongoDB: Indexes Text indexes Support efficient searching for string content in a collection Text indexes store only root words (no language- specific stop words or stem) E.g., db.reviews.createIndex( {comment: “text”} ) Wildcard ($**) allows MongoDB to index every field that contains string data E.g., db.reviews.createIndex( {“$**”: “text”} )