SlideShare a Scribd company logo
1 of 62
Download to read offline
Document-Oriented
Databases in Depth
Dr. Fabio Fumarola
Outline
• Introduction
• What is a Document
• DocumentDBs
• MongoDB
– Data Model
– Indexes
– CRUD
– Scaling
– Pros and Cons
2
Document DB introduction
• Documents are the main concept.
• A Document-oriented database stores and retrieves
documents (XML, JSON, BSON and so on).
• Documents are:
– Self-describing
– Hierarchical tree data structures (maps, collection and
scalar values)
3
What is a Document DB?
• Document databases store documents in the value
part of the key-value store where:
– Documents are indexed using a BTree
– and queried using a JavaScript query engine
4
What is a Document DB?
• Documents have differences in their attributes
• But belongs to the same collection
• This is different from relational databases where columns:
– Stores the same type of values
– Or null
5
{
"name": "Phil",
"age": 26,
"status": "A"
}
{
"name": "Phil",
"age": 26,
"status": "A",
"citiesVisited" : ["Chicago", "LA", "San
Francisco"]
}
RDBMS vs Document DB:
Terminology
6
Document DBs
• MongoDB
• CouchDB
• RethinkDB
• RavenDB
7
Mongo DB
8
Documents: Data Model
9
Documents: Data Model
• Data has a flexible schema
• This helps in matching document to objects
– Each document can match the fields of a document also if
with different structure
• Data is represented as a map
• Relations can be represented as: references and
embedded documents
10
Documents: Structure
References
11
Documents: Structure Embedded
12
Documents: Write Operations
• Writes are atomic at the document level
– A Denormalized data model facilitates atomic write
operations.
– Normalizing the data over multiple collection would
require multiple write operation that are not atomic.
13
Documents: Growth
• Each time a document is updated the modification
are done changing affected attributes
• Each document has a maximum size of 16MB
• If the document size exceeds MongoDB relocates the
document on disk.
• In MongoDB 3.0 this problem is minimized using the
Power of 2 Sized Allocation
14
Documents: ObjectId
• ObjectId is a 12-byte BSON type, constructed using:
– a 4-byte value representing the seconds since the Unix
epoch,
– a 3-byte machine identifier,
– a 2-byte process id, and
– a 3-byte counter, starting with a random value.
• It is an interesting approach to generate keys
considering that documents are retrieved using
document queries
15
Documents: Indexing
16
Documents: Indexing
• Indexes allows efficient queries on MongoDB.
• They are used to limit the number of documents to
inspect
• Otherwise, it has to scan every document in a
collection.
• By default MongoDB create indexes only on the _id
field
17
Documents: Indexing
• Indexes are created using B-tree and stores data of
fields ordered by values.
• In addition MongoDB returns sorted results by using
the index.
18
Documents: Indexing
19
Documents: Index Types
• Single Field
20
Documents: Index Types
• Compound Index: indexed by attributes (left to right)
21
Documents: Index Types
Multikey Index:
•to index content in arrays
22
Documents: Index Types
• Geospatial Index: 2d and 2sphere indexes
• Text Indexes: performs tokenization, stopwords
removal and stemming.
• Hashed Indexes: used to provide an hash based
sharding
23
Documents: CRUD
24
Query a Collection
25
Queries
• Queries specify criteria, or condition that identify
documents
• A query may include projections to specify the fields
to return.
• It is possible to impose limits, skips and sort orders.
26
Query Interface
27
The same query in SQL
Query Behavior
• All queries in MongoDB address a single collection.
• We impose limits, skips, and sort orders.
• The order of documents returned by a query is not defined
unless you specify a sort().
• Operations that modify existing documents (i.e. updates) use
the same query syntax as queries to select documents to
update.
• In aggregation pipeline, the $match pipeline stage provides
access to MongoDB queries.
28
Query Statements
29
Projections
30
Cursors
• Each Query like db.collection.find() returns a cursor
• To access the documents, you need to iterate the
cursor.
• By default MongDB closes a cursor
– after 10 minutes of inactivity
– or if the client has exhausted the cursor
31
var myCursor = db.inventory.find().addOption(DBQuery.Option.noTimeout);
Data Modification
• Data modification refers to operations that create,
update, or delete data. In MongoDB.
• These operations modify the data of a single
collection.
• For the update and delete operations, it is possible
to specify the criteria to select the documents to
update or remove.
32
Insert
33
Which corresponds to the SQL query
Update
34
Update: Example
35
Update Behavior
• By default, the db.collection.update() method updates a
single document.
• However, with the multi option, update() can update all
documents in a collection.
• If the update() method includes upsert: true and no
documents match the query portion of the update operation,
then the update operation creates a new document.
36
Remove
37
Remove Behavior
• By default, db.collection.remove() method removes
all documents that match its query.
• However, the method can accept a flag to limit the
delete operation to a single document.
38
Write Concern
• There are different levels of guarantee for writes
• When inserts, updates and deletes have a weak write
concern, write operations return quickly.
• In some failure cases, write operations issued with weak write
concerns may not persist.
• With stronger write concerns, clients wait after sending a
write operation for MongoDB to confirm the write
operations.
39
Unacknowledged: Default Write
40
Acknowledged
• This is defined in the drivers
41
Journaled
• Operation are acknowledges only after operations are saved
to the journal.
42
Replica Acknowledged
43
Scaling
44
Scaling
• The idea of scaling is to add more node to the cluster
of nodes.
• There are two different context to consider:
– Heavy reads
– Heavy writes
45
Scaling: Heavy Reads
• Scaling here can be achieved by adding more read slaves
• All the reads can be directed to the slaves.
• When a node is added it will sync with the other nodes.
• The advantage of this setting is that we do not need to stop
the cluster.
46
rs.add(“mongo_address:27017”)
Data Replication
47
Automatic Failover
48
Scaling: Heavy Writes
• We can start using the Sharding feature.
• Sharding, or horizontal scaling divides the data set
and distributes the data over multiple servers.
• Each shard is an independent database, and
collectively, the shards make up a single logical
database.
49
Sharding
50
Sharding in MongoDB
• Shard: store the data
• Query Routers: interface to
client and direct queries
• Config Server: store
cluster’s metadata.
51
Range Based Sharding
• MongoDB divides the data set into ranges determined by the
shard key values to provide range based partitioning.
52
Hash Based Sharding
• MongoDB computes a hash of a field’s value, and then uses
these hashes to create chunks
53
Performance Comparison
• Range based partitioning supports more efficient range
queries.
• However, range based partitioning can result in an uneven
distribution of data.
• Hash based partitioning, by contrast, ensures an even
distribution of data at the expense of efficient range queries.
54
Other DocumentDBs
55
CouchDB
• Written in Erlang.
• Documents are stored using JSON.
• The query language is in Javascript and supports MapReduce
integration.
• One of its distinguishing features is multi-master replication.
• ACID: It implements a form of Multi-Version Concurrency
Control (MVCC) in order to avoid the need to lock the
database file during writes.
• CouchDB does not guarantees (eventual) consistency to be
able to provide both availability and partition tolerance.
56
CouchDB: Features
• Master-Master Replication - Because of the append-
only style of commits.
• Reliability of the actual data store backing the DB
(Log Files)
• Mobile platform support. CouchDB actually has
installs for iOS and Android.
• HTTP REST JSON interaction only. No binary protocol
57
RethinkDB
• The RethinkDB server is written in C++ and runs on
32-bit and 64-bit Linux systems.
• It is a JSON based database.
• It is characterized by push update queries.
• It supports a MapReduce style API, and geospatial
queries.
58
r.table('tv_shows').insert([{ name: 'Star Trek TNG', episodes: 178 },
{ name: 'Battlestar Galactica', episodes: 75 }])
RavenDB
• It is a DocumentDB written in C#
59
Document Store: Advantages
• Documents are independent units
• Application logic is easier to write. (JSON).
• Schema Free:
– Unstructured data can be stored easily, since a document
contains whatever keys and values the application logic
requires.
– In addition, costly migrations are avoided since the
database does not need to know its information schema in
advance.
60
Suitable Use Cases
• Event Logging: where we need to store different types of
event (order_processed, customer_logged).
• Content Management System: because the schema-free
approach is well suited
• Web analytics or Real-Time Analytics: useful to update
counters, page views and metrics in general.
61
When Not to Use
• Complex Transactions: when you need atomic cross-
document operations, but we can use RavenDB or RethinkDB
• Queries against Varying Aggregate Structure: that is when
the structure of your aggregates vary because of data
continuous data evolutions
62

More Related Content

What's hot

Introduction to NoSQL Databases
Introduction to NoSQL DatabasesIntroduction to NoSQL Databases
Introduction to NoSQL DatabasesDerek Stainer
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Databasenehabsairam
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBRavi Teja
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational DatabasesChris Baglieri
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDBvaluebound
 
Introduction to MongoDB.pptx
Introduction to MongoDB.pptxIntroduction to MongoDB.pptx
Introduction to MongoDB.pptxSurya937648
 
MongoDB.pptx
MongoDB.pptxMongoDB.pptx
MongoDB.pptxSigit52
 
Data Modeling for MongoDB
Data Modeling for MongoDBData Modeling for MongoDB
Data Modeling for MongoDBMongoDB
 
Big Data Architecture
Big Data ArchitectureBig Data Architecture
Big Data ArchitectureGuido Schmutz
 
No SQL- The Future Of Data Storage
No SQL- The Future Of Data StorageNo SQL- The Future Of Data Storage
No SQL- The Future Of Data StorageBethmi Gunasekara
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignMongoDB
 
When to Use MongoDB
When to Use MongoDBWhen to Use MongoDB
When to Use MongoDBMongoDB
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databasesJames Serra
 
Introduction to NOSQL databases
Introduction to NOSQL databasesIntroduction to NOSQL databases
Introduction to NOSQL databasesAshwani Kumar
 

What's hot (20)

Introduction to NoSQL Databases
Introduction to NoSQL DatabasesIntroduction to NoSQL Databases
Introduction to NoSQL Databases
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Database
 
MongoDB
MongoDBMongoDB
MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Apache HBase™
Apache HBase™Apache HBase™
Apache HBase™
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational Databases
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDB
 
Introduction to MongoDB.pptx
Introduction to MongoDB.pptxIntroduction to MongoDB.pptx
Introduction to MongoDB.pptx
 
MongoDB.pptx
MongoDB.pptxMongoDB.pptx
MongoDB.pptx
 
Mongo db intro.pptx
Mongo db intro.pptxMongo db intro.pptx
Mongo db intro.pptx
 
Data Modeling for MongoDB
Data Modeling for MongoDBData Modeling for MongoDB
Data Modeling for MongoDB
 
Mongo DB
Mongo DB Mongo DB
Mongo DB
 
Big Data Architecture
Big Data ArchitectureBig Data Architecture
Big Data Architecture
 
Mongodb
MongodbMongodb
Mongodb
 
No SQL- The Future Of Data Storage
No SQL- The Future Of Data StorageNo SQL- The Future Of Data Storage
No SQL- The Future Of Data Storage
 
Hadoop Map Reduce
Hadoop Map ReduceHadoop Map Reduce
Hadoop Map Reduce
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
When to Use MongoDB
When to Use MongoDBWhen to Use MongoDB
When to Use MongoDB
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databases
 
Introduction to NOSQL databases
Introduction to NOSQL databasesIntroduction to NOSQL databases
Introduction to NOSQL databases
 

Viewers also liked

NoSQL databases pros and cons
NoSQL databases pros and consNoSQL databases pros and cons
NoSQL databases pros and consFabio Fumarola
 
Types of databases
Types of databasesTypes of databases
Types of databasesPAQUIAAIZEL
 
7. Key-Value Databases: In Depth
7. Key-Value Databases: In Depth7. Key-Value Databases: In Depth
7. Key-Value Databases: In DepthFabio Fumarola
 
Introduction to Graph Databases
Introduction to Graph DatabasesIntroduction to Graph Databases
Introduction to Graph DatabasesMax De Marzi
 
Graph database Use Cases
Graph database Use CasesGraph database Use Cases
Graph database Use CasesMax De Marzi
 
Common MongoDB Use Cases
Common MongoDB Use Cases Common MongoDB Use Cases
Common MongoDB Use Cases MongoDB
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMike Friedman
 
Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...
Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...
Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...Peter O'Kelly
 
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0Tugdual Grall
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented databaseWojciech Sznapka
 
10b. Graph Databases Lab
10b. Graph Databases Lab10b. Graph Databases Lab
10b. Graph Databases LabFabio Fumarola
 
8. column oriented databases
8. column oriented databases8. column oriented databases
8. column oriented databasesFabio Fumarola
 
9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab9b. Document-Oriented Databases lab
9b. Document-Oriented Databases labFabio Fumarola
 
8b. Column Oriented Databases Lab
8b. Column Oriented Databases Lab8b. Column Oriented Databases Lab
8b. Column Oriented Databases LabFabio Fumarola
 
8a. How To Setup HBase with Docker
8a. How To Setup HBase with Docker8a. How To Setup HBase with Docker
8a. How To Setup HBase with DockerFabio Fumarola
 

Viewers also liked (20)

NoSQL databases pros and cons
NoSQL databases pros and consNoSQL databases pros and cons
NoSQL databases pros and cons
 
Types of databases
Types of databasesTypes of databases
Types of databases
 
7. Key-Value Databases: In Depth
7. Key-Value Databases: In Depth7. Key-Value Databases: In Depth
7. Key-Value Databases: In Depth
 
Introduction to Graph Databases
Introduction to Graph DatabasesIntroduction to Graph Databases
Introduction to Graph Databases
 
Graph database Use Cases
Graph database Use CasesGraph database Use Cases
Graph database Use Cases
 
Common MongoDB Use Cases
Common MongoDB Use Cases Common MongoDB Use Cases
Common MongoDB Use Cases
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...
Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...
Revisiting Open Document Format and Office Open XML: The Quiet Revolution Con...
 
MapReduce and NoSQL
MapReduce and NoSQLMapReduce and NoSQL
MapReduce and NoSQL
 
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
 
Nosql
NosqlNosql
Nosql
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
Hbase an introduction
Hbase an introductionHbase an introduction
Hbase an introduction
 
10b. Graph Databases Lab
10b. Graph Databases Lab10b. Graph Databases Lab
10b. Graph Databases Lab
 
8. column oriented databases
8. column oriented databases8. column oriented databases
8. column oriented databases
 
9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab
 
8b. Column Oriented Databases Lab
8b. Column Oriented Databases Lab8b. Column Oriented Databases Lab
8b. Column Oriented Databases Lab
 
3 Git
3 Git3 Git
3 Git
 
8a. How To Setup HBase with Docker
8a. How To Setup HBase with Docker8a. How To Setup HBase with Docker
8a. How To Setup HBase with Docker
 
10. Graph Databases
10. Graph Databases10. Graph Databases
10. Graph Databases
 

Similar to 9. Document Oriented Databases

TechEd AU 2014: Microsoft Azure DocumentDB Deep Dive
TechEd AU 2014: Microsoft Azure DocumentDB Deep DiveTechEd AU 2014: Microsoft Azure DocumentDB Deep Dive
TechEd AU 2014: Microsoft Azure DocumentDB Deep DiveIntergen
 
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 behlTO THE NEW | Technology
 
Introduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQLIntroduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQLMayur Patil
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBSean Laurent
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewAntonio Pintus
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB InternalsSiraj Memon
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongoMichael Bright
 
Using MongoDB and Python
Using MongoDB and PythonUsing MongoDB and Python
Using MongoDB and PythonMike Bright
 
Introduction to Azure DocumentDB
Introduction to Azure DocumentDBIntroduction to Azure DocumentDB
Introduction to Azure DocumentDBRadenko Zec
 

Similar to 9. Document Oriented Databases (20)

MongoDB_ppt.pptx
MongoDB_ppt.pptxMongoDB_ppt.pptx
MongoDB_ppt.pptx
 
No sql Database
No sql DatabaseNo sql Database
No sql Database
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB
MongoDBMongoDB
MongoDB
 
TechEd AU 2014: Microsoft Azure DocumentDB Deep Dive
TechEd AU 2014: Microsoft Azure DocumentDB Deep DiveTechEd AU 2014: Microsoft Azure DocumentDB Deep Dive
TechEd AU 2014: Microsoft Azure DocumentDB Deep Dive
 
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
 
Mongo db
Mongo dbMongo db
Mongo db
 
Introduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQLIntroduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQL
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
 
mongodb_DS.pptx
mongodb_DS.pptxmongodb_DS.pptx
mongodb_DS.pptx
 
Nosql part 2
Nosql part 2Nosql part 2
Nosql part 2
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB Internals
 
MongoDB
MongoDBMongoDB
MongoDB
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo
 
Using MongoDB and Python
Using MongoDB and PythonUsing MongoDB and Python
Using MongoDB and Python
 
No sq lv1_0
No sq lv1_0No sq lv1_0
No sq lv1_0
 
Mongo db 3.4 Overview
Mongo db 3.4 OverviewMongo db 3.4 Overview
Mongo db 3.4 Overview
 
Introduction to Azure DocumentDB
Introduction to Azure DocumentDBIntroduction to Azure DocumentDB
Introduction to Azure DocumentDB
 
Wmware NoSQL
Wmware NoSQLWmware NoSQL
Wmware NoSQL
 

More from Fabio Fumarola

11. From Hadoop to Spark 2/2
11. From Hadoop to Spark 2/211. From Hadoop to Spark 2/2
11. From Hadoop to Spark 2/2Fabio Fumarola
 
11. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:211. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:2Fabio Fumarola
 
8. key value databases laboratory
8. key value databases laboratory 8. key value databases laboratory
8. key value databases laboratory Fabio Fumarola
 
6 Data Modeling for NoSQL 2/2
6 Data Modeling for NoSQL 2/26 Data Modeling for NoSQL 2/2
6 Data Modeling for NoSQL 2/2Fabio Fumarola
 
5 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/25 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/2Fabio Fumarola
 
2 Linux Container and Docker
2 Linux Container and Docker2 Linux Container and Docker
2 Linux Container and DockerFabio Fumarola
 
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...Fabio Fumarola
 
An introduction to maven gradle and sbt
An introduction to maven gradle and sbtAn introduction to maven gradle and sbt
An introduction to maven gradle and sbtFabio Fumarola
 
Develop with linux containers and docker
Develop with linux containers and dockerDevelop with linux containers and docker
Develop with linux containers and dockerFabio Fumarola
 
Linux containers and docker
Linux containers and dockerLinux containers and docker
Linux containers and dockerFabio Fumarola
 
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce Fabio Fumarola
 

More from Fabio Fumarola (13)

11. From Hadoop to Spark 2/2
11. From Hadoop to Spark 2/211. From Hadoop to Spark 2/2
11. From Hadoop to Spark 2/2
 
11. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:211. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:2
 
8. key value databases laboratory
8. key value databases laboratory 8. key value databases laboratory
8. key value databases laboratory
 
6 Data Modeling for NoSQL 2/2
6 Data Modeling for NoSQL 2/26 Data Modeling for NoSQL 2/2
6 Data Modeling for NoSQL 2/2
 
5 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/25 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/2
 
2 Linux Container and Docker
2 Linux Container and Docker2 Linux Container and Docker
2 Linux Container and Docker
 
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
 
Scala and spark
Scala and sparkScala and spark
Scala and spark
 
An introduction to maven gradle and sbt
An introduction to maven gradle and sbtAn introduction to maven gradle and sbt
An introduction to maven gradle and sbt
 
Develop with linux containers and docker
Develop with linux containers and dockerDevelop with linux containers and docker
Develop with linux containers and docker
 
Linux containers and docker
Linux containers and dockerLinux containers and docker
Linux containers and docker
 
08 datasets
08 datasets08 datasets
08 datasets
 
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce
 

Recently uploaded

Statistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdfStatistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdfnikeshsingh56
 
Presentation of project of business person who are success
Presentation of project of business person who are successPresentation of project of business person who are success
Presentation of project of business person who are successPratikSingh115843
 
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...Dr Arash Najmaei ( Phd., MBA, BSc)
 
DATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etcDATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etclalithasri22
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksdeepakthakur548787
 
Digital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdfDigital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdfNicoChristianSunaryo
 
Role of Consumer Insights in business transformation
Role of Consumer Insights in business transformationRole of Consumer Insights in business transformation
Role of Consumer Insights in business transformationAnnie Melnic
 
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...Jack Cole
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfblazblazml
 
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Boston Institute of Analytics
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBoston Institute of Analytics
 
Non Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdfNon Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdfPratikPatil591646
 
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelDecoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelBoston Institute of Analytics
 
IBEF report on the Insurance market in India
IBEF report on the Insurance market in IndiaIBEF report on the Insurance market in India
IBEF report on the Insurance market in IndiaManalVerma4
 

Recently uploaded (17)

2023 Survey Shows Dip in High School E-Cigarette Use
2023 Survey Shows Dip in High School E-Cigarette Use2023 Survey Shows Dip in High School E-Cigarette Use
2023 Survey Shows Dip in High School E-Cigarette Use
 
Statistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdfStatistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdf
 
Presentation of project of business person who are success
Presentation of project of business person who are successPresentation of project of business person who are success
Presentation of project of business person who are success
 
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
 
DATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etcDATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etc
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing works
 
Data Analysis Project: Stroke Prediction
Data Analysis Project: Stroke PredictionData Analysis Project: Stroke Prediction
Data Analysis Project: Stroke Prediction
 
Digital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdfDigital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdf
 
Role of Consumer Insights in business transformation
Role of Consumer Insights in business transformationRole of Consumer Insights in business transformation
Role of Consumer Insights in business transformation
 
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
 
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
 
Non Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdfNon Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdf
 
Insurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis ProjectInsurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis Project
 
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelDecoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
 
IBEF report on the Insurance market in India
IBEF report on the Insurance market in IndiaIBEF report on the Insurance market in India
IBEF report on the Insurance market in India
 

9. Document Oriented Databases

  • 2. Outline • Introduction • What is a Document • DocumentDBs • MongoDB – Data Model – Indexes – CRUD – Scaling – Pros and Cons 2
  • 3. Document DB introduction • Documents are the main concept. • A Document-oriented database stores and retrieves documents (XML, JSON, BSON and so on). • Documents are: – Self-describing – Hierarchical tree data structures (maps, collection and scalar values) 3
  • 4. What is a Document DB? • Document databases store documents in the value part of the key-value store where: – Documents are indexed using a BTree – and queried using a JavaScript query engine 4
  • 5. What is a Document DB? • Documents have differences in their attributes • But belongs to the same collection • This is different from relational databases where columns: – Stores the same type of values – Or null 5 { "name": "Phil", "age": 26, "status": "A" } { "name": "Phil", "age": 26, "status": "A", "citiesVisited" : ["Chicago", "LA", "San Francisco"] }
  • 6. RDBMS vs Document DB: Terminology 6
  • 7. Document DBs • MongoDB • CouchDB • RethinkDB • RavenDB 7
  • 10. Documents: Data Model • Data has a flexible schema • This helps in matching document to objects – Each document can match the fields of a document also if with different structure • Data is represented as a map • Relations can be represented as: references and embedded documents 10
  • 13. Documents: Write Operations • Writes are atomic at the document level – A Denormalized data model facilitates atomic write operations. – Normalizing the data over multiple collection would require multiple write operation that are not atomic. 13
  • 14. Documents: Growth • Each time a document is updated the modification are done changing affected attributes • Each document has a maximum size of 16MB • If the document size exceeds MongoDB relocates the document on disk. • In MongoDB 3.0 this problem is minimized using the Power of 2 Sized Allocation 14
  • 15. Documents: ObjectId • ObjectId is a 12-byte BSON type, constructed using: – a 4-byte value representing the seconds since the Unix epoch, – a 3-byte machine identifier, – a 2-byte process id, and – a 3-byte counter, starting with a random value. • It is an interesting approach to generate keys considering that documents are retrieved using document queries 15
  • 17. Documents: Indexing • Indexes allows efficient queries on MongoDB. • They are used to limit the number of documents to inspect • Otherwise, it has to scan every document in a collection. • By default MongoDB create indexes only on the _id field 17
  • 18. Documents: Indexing • Indexes are created using B-tree and stores data of fields ordered by values. • In addition MongoDB returns sorted results by using the index. 18
  • 20. Documents: Index Types • Single Field 20
  • 21. Documents: Index Types • Compound Index: indexed by attributes (left to right) 21
  • 22. Documents: Index Types Multikey Index: •to index content in arrays 22
  • 23. Documents: Index Types • Geospatial Index: 2d and 2sphere indexes • Text Indexes: performs tokenization, stopwords removal and stemming. • Hashed Indexes: used to provide an hash based sharding 23
  • 26. Queries • Queries specify criteria, or condition that identify documents • A query may include projections to specify the fields to return. • It is possible to impose limits, skips and sort orders. 26
  • 28. Query Behavior • All queries in MongoDB address a single collection. • We impose limits, skips, and sort orders. • The order of documents returned by a query is not defined unless you specify a sort(). • Operations that modify existing documents (i.e. updates) use the same query syntax as queries to select documents to update. • In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. 28
  • 31. Cursors • Each Query like db.collection.find() returns a cursor • To access the documents, you need to iterate the cursor. • By default MongDB closes a cursor – after 10 minutes of inactivity – or if the client has exhausted the cursor 31 var myCursor = db.inventory.find().addOption(DBQuery.Option.noTimeout);
  • 32. Data Modification • Data modification refers to operations that create, update, or delete data. In MongoDB. • These operations modify the data of a single collection. • For the update and delete operations, it is possible to specify the criteria to select the documents to update or remove. 32
  • 36. Update Behavior • By default, the db.collection.update() method updates a single document. • However, with the multi option, update() can update all documents in a collection. • If the update() method includes upsert: true and no documents match the query portion of the update operation, then the update operation creates a new document. 36
  • 38. Remove Behavior • By default, db.collection.remove() method removes all documents that match its query. • However, the method can accept a flag to limit the delete operation to a single document. 38
  • 39. Write Concern • There are different levels of guarantee for writes • When inserts, updates and deletes have a weak write concern, write operations return quickly. • In some failure cases, write operations issued with weak write concerns may not persist. • With stronger write concerns, clients wait after sending a write operation for MongoDB to confirm the write operations. 39
  • 41. Acknowledged • This is defined in the drivers 41
  • 42. Journaled • Operation are acknowledges only after operations are saved to the journal. 42
  • 45. Scaling • The idea of scaling is to add more node to the cluster of nodes. • There are two different context to consider: – Heavy reads – Heavy writes 45
  • 46. Scaling: Heavy Reads • Scaling here can be achieved by adding more read slaves • All the reads can be directed to the slaves. • When a node is added it will sync with the other nodes. • The advantage of this setting is that we do not need to stop the cluster. 46 rs.add(“mongo_address:27017”)
  • 49. Scaling: Heavy Writes • We can start using the Sharding feature. • Sharding, or horizontal scaling divides the data set and distributes the data over multiple servers. • Each shard is an independent database, and collectively, the shards make up a single logical database. 49
  • 51. Sharding in MongoDB • Shard: store the data • Query Routers: interface to client and direct queries • Config Server: store cluster’s metadata. 51
  • 52. Range Based Sharding • MongoDB divides the data set into ranges determined by the shard key values to provide range based partitioning. 52
  • 53. Hash Based Sharding • MongoDB computes a hash of a field’s value, and then uses these hashes to create chunks 53
  • 54. Performance Comparison • Range based partitioning supports more efficient range queries. • However, range based partitioning can result in an uneven distribution of data. • Hash based partitioning, by contrast, ensures an even distribution of data at the expense of efficient range queries. 54
  • 56. CouchDB • Written in Erlang. • Documents are stored using JSON. • The query language is in Javascript and supports MapReduce integration. • One of its distinguishing features is multi-master replication. • ACID: It implements a form of Multi-Version Concurrency Control (MVCC) in order to avoid the need to lock the database file during writes. • CouchDB does not guarantees (eventual) consistency to be able to provide both availability and partition tolerance. 56
  • 57. CouchDB: Features • Master-Master Replication - Because of the append- only style of commits. • Reliability of the actual data store backing the DB (Log Files) • Mobile platform support. CouchDB actually has installs for iOS and Android. • HTTP REST JSON interaction only. No binary protocol 57
  • 58. RethinkDB • The RethinkDB server is written in C++ and runs on 32-bit and 64-bit Linux systems. • It is a JSON based database. • It is characterized by push update queries. • It supports a MapReduce style API, and geospatial queries. 58 r.table('tv_shows').insert([{ name: 'Star Trek TNG', episodes: 178 }, { name: 'Battlestar Galactica', episodes: 75 }])
  • 59. RavenDB • It is a DocumentDB written in C# 59
  • 60. Document Store: Advantages • Documents are independent units • Application logic is easier to write. (JSON). • Schema Free: – Unstructured data can be stored easily, since a document contains whatever keys and values the application logic requires. – In addition, costly migrations are avoided since the database does not need to know its information schema in advance. 60
  • 61. Suitable Use Cases • Event Logging: where we need to store different types of event (order_processed, customer_logged). • Content Management System: because the schema-free approach is well suited • Web analytics or Real-Time Analytics: useful to update counters, page views and metrics in general. 61
  • 62. When Not to Use • Complex Transactions: when you need atomic cross- document operations, but we can use RavenDB or RethinkDB • Queries against Varying Aggregate Structure: that is when the structure of your aggregates vary because of data continuous data evolutions 62

Editor's Notes

  1. Fundamentally, indexes in MongoDB are similar to indexes in other database systems. MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the documents in a MongoDB collection.
  2. Master-Master Couch does every modification to the DB is considered a revision making conflicts during replication much less likely and allowing for some awesome master-master replication or what Cassandra calls a "ring" of servers all bi-directionally replicating to each other. It can even look more like a fully connected graph of replication rules. Reliability: Because CouchDB records any changes as a "revision" to a document and appends them to the DB file on disk, the file can be copied or snapshotted at any time even while the DB is running and you don't have to worry about corruption. It is a really resilient method of storage.