SlideShare uma empresa Scribd logo
1 de 31
NDBI040: Big Data Management and NoSQL
Databases
http://www.ksi.mff.cuni.cz/~svoboda/courses/171-
NDBI040/
Practical Class 8
MongoDB
Martin Svoboda
svoboda@ksi.mff.cuni.
cz
5. 12. 2017
Charles University in Prague, Faculty of Mathematics and
Physics
Czech Technical University in Prague, Faculty of Electrical
Data Model
Database system structure
Instance → databases → collections → documents
• Database
• Collection
Collection of documents, usually of a similar structure
• Document
MongoDB document = one JSON object
– I.e. even a complex JSON object with other recursively
nested objects, arrays or values
Unique immutable identifier _id
Field name restrictions: _id, $,.
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 2
CRUD Operations
Overview
• db.collection.insert()
Inserts a new document into a collection
• db.collection.update()
Modifies an existing document / documents or inserts
a new one
• db.collection.remove()
Deletes an existing document / documents
• db.collection.find()
Finds documents based on filtering conditions
Projection and / or sorting may be applied too
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 3
Mongo Shell
Connect to our NoSQL server
• SSH / PuTTY and SFTP / WinSCP
• nosql.ms.mff.cuni.cz:42222 Start
mongo shell
• mongo
Try several basic commands
• help
Displays a brief description of database commands
• exit
quit()
Closes the current client connection
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 4
Databases
Switch to your database
• use login
db = db.getSiblingDB('login')
Use your login name as a name for your database
List all the existing databases
• show databases
• show dbs
db.adminCommand('listDatabases')
Your database will be created later on implicitly
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 5
Collections
Create a new collection for actors
• db.createCollection("actors")
Suitable when creating collections with specific options
since collections can also be created implicitly
List all collections in your database
• show collections
db.getCollectionNames()
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 6
Insert Operation
Inserts a new document / documents into a given collection
db . ollection
collection . nsert
insert
( document
document
[ document
document
,
]
, options
options
)
• Parameters
Document: one or more documents to be inserted
Options
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 7
Insert Operation
Insert a few new documents into the collection of actors
db.actors.insert({ _id: "trojan", name: "Ivan Trojan" })
db.actors.insert({ _id: 2, name: "Jiri Machacek" })
db.actors.insert({ _id: ObjectId(), name: "Jitka Schneiderova" })
db.actors.insert({ name: "Zdenek Sverak" })
Retrieve all documents from the collection of actors
db.actors.find()
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 8
Update Operation
Modifies / replaces an existing document / documents
db . ollection
collection . update
update
( query
query , update
update
options
, options
)
• Parameters
Query: description of documents to be updated
Update: modification actions to be applied
Options
Update operators
• $set, $unset, $rename, $inc, $mul, $currentDate,
$push, $addToSet, $pop, $pull, …
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 9
Update Operation
Update the document of actor Ivan Trojan
db.actors.update(
{ _id: "trojan" },
{ name: "Ivan Trojan", year: 1964 }
)
db.actors.update(
{ name: "Ivan Trojan", year: { $lt: 2000 } },
{ name: "Ivan Trojan", year: 1964 }
)
• At most one document is updated
• Its content is replaced with a new value
Check the current content of the document
db.actors.find({ _id: "trojan" })
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 10
Update Operation
Use updatemethod to insert a new actor
• Inserts a new document when upsertbehavior was
enabled and no document could be updated
db.actors.update(
{ _id: "geislerova" },
{ name: "Anna Geislerova" },
{ upsert: true }
)
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 11
Update Operation
Try to modify the document identifier of an existing
document
• Your request will be rejected since
document identifiers are immutable
db.actors.update(
{ _id: "trojan" },
{ _id: 1, name: "Ivan Trojan", year: 1964 }
)
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 12
Update Operation
Update the document of actor Ivan Trojan
db.actors.update(
{ _id: "trojan" },
{
$set: { year: 1964, age: 52 },
$inc: { rating: 1 },
$push: { movies: { $each: [ "samotari", "medvidek" ] } }
}
)
Update multiple documents at once
db.actors.update(
{ year: { $lt: 2000 } },
{ $set: { rating: 3 } },
{ multi: true }
)
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 13
Save Operation
Replaces an existing / inserts a new document
db . ollection
collection save
. save
( document
document
options
, options
)
• Parameters
Document: document to be modified / inserted
Options
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 14
Save Operation
Use savemethod to insert new actors
• Document identifier must not be specified in the query
or must not yet exist in the collection
db.actors.save({ name: "Tatiana Vilhelmova" })
db.actors.save({ _id: 6, name: "Sasa Rasilov" })
Use savemethod to update actor Ivan Trojan
• Document identifier must be specified in the query and
must exist in the collection
db.actors.save({ _id: "trojan", name: "Ivan Trojan", year: 1964 })
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 15
Remove Operation
Removes a document / documents from a given collection
db . ollection
collection . remove
remove
( query
query
options
, options
)
• Parameters
Query: description of documents to be removed
Options
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 16
Remove Operation
Remove selected documents from the collection of actors
db.actors.remove({ _id: "geislerova" })
db.actors.remove(
{ year: { $lt: 2000 } },
{ justOne: true }
)
Remove all the documents from the collection of actors
db.actors.remove({ })
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 17
Sample Data
Insert the following actors into your emptied collection
{ _id: "trojan",
name: "Ivan Trojan", year: 1964, movies: [
"samotari", "medvidek" ] }
{ _id: "machacek",
name: "Jiri Machacek", year: 1966,
movies: [ "medvidek", "vratnelahve", "samotari" ] }
{ _id: "schneiderova",
name: "Jitka Schneiderova", year: 1973,
movies: [ "samotari" ] }
{ _id: "sverak",
name: "Zdenek Sverak", year: 1936,
movies: [ "vratnelahve" ] }
{ _id: "geislerova",
name: "Anna Geislerova", year: 1976 }
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 18
Find Operation
Selects documents from a given collection
db . ollection
collection find
. find
( query
query
projection
, projection
)
• Parameters
Query: description of documents to be selected
Projection: fields to be included / excluded in theresult
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 19
Querying
Execute and explain the meaning of the following queries
db.actors.find()
db.actors.find({ })
db.actors.find({ _id: "trojan" })
db.actors.find({ name: "Ivan Trojan", year: 1964 })
db.actors.find({ year: { $gte: 1960, $lte: 1980 } })
db.actors.find({ movies: { $exists: true } })
db.actors.find({ movies: "medvidek" })
db.actors.find({ movies: { $in: [ "medvidek", "pelisky" ] } })
db.actors.find({ movies: { $all: [ "medvidek", "pelisky" ] } })
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 20
Querying
Execute and explain the meaning of the following queries
db.actors.find({ $or: [ { year: 1964 }, { rating: { $gte: 3 } } ] })
db.actors.find({ rating: { $not: { $gte: 3 } } })
db.actors.find({ }, { name: 1, year: 1 })
db.actors.find({ }, { movies: 0, _id: 0 })
db.actors.find({ }, { name: 1, movies: { $slice: 2 }, _id: 0 })
db.actors.find().sort({ year: 1, name: -1 })
db.actors.find().sort({ name: 1 }).skip(1).limit(2)
db.actors.find().sort({ name: 1 }).limit(2).skip(1)
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 21
Index Structures
Motivation
• Full collection scan must be conducted when
searching for documents unless an appropriate
index exists
Primary index
• Unique index on values of the _id field
• Created automatically
Secondary indexes
• Created manually for values of a given key field / fields
• Always within just a single collection
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 22
Index Structures
Secondary index creation
db . ollection
collection . createIndex
createIndex key
s
( keys
, options
options
)
Definition of keys (fields) to be involved
ield
{ field : 1
text
hashed
2dsphere
-1
text
hashed
2d
2dsphere
,
}
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 23
Index Structures
Index types
• 1, -1– standard ascending / descending value indexes
Both scalar values and embedded documents can be
indexed
• hashed– hash values of a single field are indexed
• text– basic full-text index
• 2d– points in planar geometry
• 2dsphere– points in spherical geometry
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 24
Index Structures
Index forms
• One key / multiple keys (composed index)
• Ordinary fields / array fields (multi-key index)
Index properties
• Unique – duplicate values are rejected (cannot be
inserted)
• Partial – only certain documents are indexed
• Sparse – documents without a given field are ignored
• TTL – documents are removed when a timeout elapses
Just some type / form / property combinations can be used!
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 25
Index Structures
Execute the following query and study its execution plan
db.actors.find({ movies: "medvidek" })
db.actors.find({ movies: "medvidek" }).explain()
Create a multikey index for movies of actors
db.actors.createIndex({ movies: 1 })
Examine the execution plan once again
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 26
MapReduce
Executes a MapReduce job on a selected
collection
db . ollection
collection . mapReduce
mapReduce
( mapfunction
map function , reducefunction
reduce function
options
, options
)
• Parameters
Map: JavaScript implementation of the Map function
Reduce: JavaScript implementation of the Reduce
function Options
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 27
MapReduce
Map function
• Current document is accessible via this
• emit(key,value)is used for emissions
Reduce function
• Intermediate key and values are provided as
arguments
• Reduced value is published via return
Options
• query: only matching documents are considered
• sort: they are processed in a specific order
• limit: at most a given number of them is processed
• out: output is stored into a given collection
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 28
MapReduce:
Example
Count the number of movies filmed in each year, starting in
2005
db.movies.mapReduce(
function() {
emit(this.year, 1);
},
function(key, values) { return
Array.sum(values);
},
{
query: { year: { $gte: 2005 } }, sort: {
year: 1 },
out: "statistics"
}
)
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 29
MapReduce
Implement and execute the following MapReduce jobs
• Find a list of actors (their names sorted
alphabetically) for each year (they were born)
values.sort()
Use out:{inline:1}option
• Calculate the overall number of actors for each
movie this.movies.forEach(function(m) { … })
Array.sum(values)
Use out:{inline:1}option once again
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 30
References
Documentation
• https://docs.mongodb.com/v3.
2/
NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 31

Mais conteúdo relacionado

Semelhante a MongoDB-MFF.pptx

Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)
Kai Zhao
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Alex Bilbie
 
Getting Started with MongoDB: 4 Application Designs
Getting Started with MongoDB: 4 Application DesignsGetting Started with MongoDB: 4 Application Designs
Getting Started with MongoDB: 4 Application Designs
DATAVERSITY
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
Wildan Maulana
 

Semelhante a MongoDB-MFF.pptx (20)

Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring Data
 
Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
 
Starting with MongoDB
Starting with MongoDBStarting with MongoDB
Starting with MongoDB
 
MongoDB_ppt.pptx
MongoDB_ppt.pptxMongoDB_ppt.pptx
MongoDB_ppt.pptx
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
La sql
La sqlLa sql
La sql
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
Getting Started with MongoDB: 4 Application Designs
Getting Started with MongoDB: 4 Application DesignsGetting Started with MongoDB: 4 Application Designs
Getting Started with MongoDB: 4 Application Designs
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behl
 
Hidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard LibraryHidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard Library
 
4 gouping object
4 gouping object4 gouping object
4 gouping object
 
9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query Optimizer
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
 

Último

Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
amitlee9823
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
karishmasinghjnh
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
gajnagarg
 
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
amitlee9823
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
amitlee9823
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
amitlee9823
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
gajnagarg
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
amitlee9823
 

Último (20)

Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
 
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 

MongoDB-MFF.pptx

  • 1. NDBI040: Big Data Management and NoSQL Databases http://www.ksi.mff.cuni.cz/~svoboda/courses/171- NDBI040/ Practical Class 8 MongoDB Martin Svoboda svoboda@ksi.mff.cuni. cz 5. 12. 2017 Charles University in Prague, Faculty of Mathematics and Physics Czech Technical University in Prague, Faculty of Electrical
  • 2. Data Model Database system structure Instance → databases → collections → documents • Database • Collection Collection of documents, usually of a similar structure • Document MongoDB document = one JSON object – I.e. even a complex JSON object with other recursively nested objects, arrays or values Unique immutable identifier _id Field name restrictions: _id, $,. NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 2
  • 3. CRUD Operations Overview • db.collection.insert() Inserts a new document into a collection • db.collection.update() Modifies an existing document / documents or inserts a new one • db.collection.remove() Deletes an existing document / documents • db.collection.find() Finds documents based on filtering conditions Projection and / or sorting may be applied too NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 3
  • 4. Mongo Shell Connect to our NoSQL server • SSH / PuTTY and SFTP / WinSCP • nosql.ms.mff.cuni.cz:42222 Start mongo shell • mongo Try several basic commands • help Displays a brief description of database commands • exit quit() Closes the current client connection NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 4
  • 5. Databases Switch to your database • use login db = db.getSiblingDB('login') Use your login name as a name for your database List all the existing databases • show databases • show dbs db.adminCommand('listDatabases') Your database will be created later on implicitly NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 5
  • 6. Collections Create a new collection for actors • db.createCollection("actors") Suitable when creating collections with specific options since collections can also be created implicitly List all collections in your database • show collections db.getCollectionNames() NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 6
  • 7. Insert Operation Inserts a new document / documents into a given collection db . ollection collection . nsert insert ( document document [ document document , ] , options options ) • Parameters Document: one or more documents to be inserted Options NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 7
  • 8. Insert Operation Insert a few new documents into the collection of actors db.actors.insert({ _id: "trojan", name: "Ivan Trojan" }) db.actors.insert({ _id: 2, name: "Jiri Machacek" }) db.actors.insert({ _id: ObjectId(), name: "Jitka Schneiderova" }) db.actors.insert({ name: "Zdenek Sverak" }) Retrieve all documents from the collection of actors db.actors.find() NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 8
  • 9. Update Operation Modifies / replaces an existing document / documents db . ollection collection . update update ( query query , update update options , options ) • Parameters Query: description of documents to be updated Update: modification actions to be applied Options Update operators • $set, $unset, $rename, $inc, $mul, $currentDate, $push, $addToSet, $pop, $pull, … NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 9
  • 10. Update Operation Update the document of actor Ivan Trojan db.actors.update( { _id: "trojan" }, { name: "Ivan Trojan", year: 1964 } ) db.actors.update( { name: "Ivan Trojan", year: { $lt: 2000 } }, { name: "Ivan Trojan", year: 1964 } ) • At most one document is updated • Its content is replaced with a new value Check the current content of the document db.actors.find({ _id: "trojan" }) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 10
  • 11. Update Operation Use updatemethod to insert a new actor • Inserts a new document when upsertbehavior was enabled and no document could be updated db.actors.update( { _id: "geislerova" }, { name: "Anna Geislerova" }, { upsert: true } ) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 11
  • 12. Update Operation Try to modify the document identifier of an existing document • Your request will be rejected since document identifiers are immutable db.actors.update( { _id: "trojan" }, { _id: 1, name: "Ivan Trojan", year: 1964 } ) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 12
  • 13. Update Operation Update the document of actor Ivan Trojan db.actors.update( { _id: "trojan" }, { $set: { year: 1964, age: 52 }, $inc: { rating: 1 }, $push: { movies: { $each: [ "samotari", "medvidek" ] } } } ) Update multiple documents at once db.actors.update( { year: { $lt: 2000 } }, { $set: { rating: 3 } }, { multi: true } ) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 13
  • 14. Save Operation Replaces an existing / inserts a new document db . ollection collection save . save ( document document options , options ) • Parameters Document: document to be modified / inserted Options NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 14
  • 15. Save Operation Use savemethod to insert new actors • Document identifier must not be specified in the query or must not yet exist in the collection db.actors.save({ name: "Tatiana Vilhelmova" }) db.actors.save({ _id: 6, name: "Sasa Rasilov" }) Use savemethod to update actor Ivan Trojan • Document identifier must be specified in the query and must exist in the collection db.actors.save({ _id: "trojan", name: "Ivan Trojan", year: 1964 }) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 15
  • 16. Remove Operation Removes a document / documents from a given collection db . ollection collection . remove remove ( query query options , options ) • Parameters Query: description of documents to be removed Options NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 16
  • 17. Remove Operation Remove selected documents from the collection of actors db.actors.remove({ _id: "geislerova" }) db.actors.remove( { year: { $lt: 2000 } }, { justOne: true } ) Remove all the documents from the collection of actors db.actors.remove({ }) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 17
  • 18. Sample Data Insert the following actors into your emptied collection { _id: "trojan", name: "Ivan Trojan", year: 1964, movies: [ "samotari", "medvidek" ] } { _id: "machacek", name: "Jiri Machacek", year: 1966, movies: [ "medvidek", "vratnelahve", "samotari" ] } { _id: "schneiderova", name: "Jitka Schneiderova", year: 1973, movies: [ "samotari" ] } { _id: "sverak", name: "Zdenek Sverak", year: 1936, movies: [ "vratnelahve" ] } { _id: "geislerova", name: "Anna Geislerova", year: 1976 } NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 18
  • 19. Find Operation Selects documents from a given collection db . ollection collection find . find ( query query projection , projection ) • Parameters Query: description of documents to be selected Projection: fields to be included / excluded in theresult NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 19
  • 20. Querying Execute and explain the meaning of the following queries db.actors.find() db.actors.find({ }) db.actors.find({ _id: "trojan" }) db.actors.find({ name: "Ivan Trojan", year: 1964 }) db.actors.find({ year: { $gte: 1960, $lte: 1980 } }) db.actors.find({ movies: { $exists: true } }) db.actors.find({ movies: "medvidek" }) db.actors.find({ movies: { $in: [ "medvidek", "pelisky" ] } }) db.actors.find({ movies: { $all: [ "medvidek", "pelisky" ] } }) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 20
  • 21. Querying Execute and explain the meaning of the following queries db.actors.find({ $or: [ { year: 1964 }, { rating: { $gte: 3 } } ] }) db.actors.find({ rating: { $not: { $gte: 3 } } }) db.actors.find({ }, { name: 1, year: 1 }) db.actors.find({ }, { movies: 0, _id: 0 }) db.actors.find({ }, { name: 1, movies: { $slice: 2 }, _id: 0 }) db.actors.find().sort({ year: 1, name: -1 }) db.actors.find().sort({ name: 1 }).skip(1).limit(2) db.actors.find().sort({ name: 1 }).limit(2).skip(1) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 21
  • 22. Index Structures Motivation • Full collection scan must be conducted when searching for documents unless an appropriate index exists Primary index • Unique index on values of the _id field • Created automatically Secondary indexes • Created manually for values of a given key field / fields • Always within just a single collection NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 22
  • 23. Index Structures Secondary index creation db . ollection collection . createIndex createIndex key s ( keys , options options ) Definition of keys (fields) to be involved ield { field : 1 text hashed 2dsphere -1 text hashed 2d 2dsphere , } NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 23
  • 24. Index Structures Index types • 1, -1– standard ascending / descending value indexes Both scalar values and embedded documents can be indexed • hashed– hash values of a single field are indexed • text– basic full-text index • 2d– points in planar geometry • 2dsphere– points in spherical geometry NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 24
  • 25. Index Structures Index forms • One key / multiple keys (composed index) • Ordinary fields / array fields (multi-key index) Index properties • Unique – duplicate values are rejected (cannot be inserted) • Partial – only certain documents are indexed • Sparse – documents without a given field are ignored • TTL – documents are removed when a timeout elapses Just some type / form / property combinations can be used! NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 25
  • 26. Index Structures Execute the following query and study its execution plan db.actors.find({ movies: "medvidek" }) db.actors.find({ movies: "medvidek" }).explain() Create a multikey index for movies of actors db.actors.createIndex({ movies: 1 }) Examine the execution plan once again NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 26
  • 27. MapReduce Executes a MapReduce job on a selected collection db . ollection collection . mapReduce mapReduce ( mapfunction map function , reducefunction reduce function options , options ) • Parameters Map: JavaScript implementation of the Map function Reduce: JavaScript implementation of the Reduce function Options NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 27
  • 28. MapReduce Map function • Current document is accessible via this • emit(key,value)is used for emissions Reduce function • Intermediate key and values are provided as arguments • Reduced value is published via return Options • query: only matching documents are considered • sort: they are processed in a specific order • limit: at most a given number of them is processed • out: output is stored into a given collection NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 28
  • 29. MapReduce: Example Count the number of movies filmed in each year, starting in 2005 db.movies.mapReduce( function() { emit(this.year, 1); }, function(key, values) { return Array.sum(values); }, { query: { year: { $gte: 2005 } }, sort: { year: 1 }, out: "statistics" } ) NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 29
  • 30. MapReduce Implement and execute the following MapReduce jobs • Find a list of actors (their names sorted alphabetically) for each year (they were born) values.sort() Use out:{inline:1}option • Calculate the overall number of actors for each movie this.movies.forEach(function(m) { … }) Array.sum(values) Use out:{inline:1}option once again NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 30
  • 31. References Documentation • https://docs.mongodb.com/v3. 2/ NDBI040: Big Data Management and NoSQL Databases | Practical Class 8: MongoDB | 5. 31