SlideShare a Scribd company logo
Introduction to MongoDB
What is MongoDB?
 Developed by 10gen
 Founded in 2007
 A document-oriented, NoSQL database
 Hash-based, schema-less database • No Data Definition Language
 In practice, this means you can store hashes with any keys and values that
you choose • Keys are a basic data type but in reality stored as strings
 Document Identifiers (_id) will be created for each document, field name
reserved by system
Cont..
 Application tracks the schema and mapping • Uses BSON format
 Based on JSON
 Written in C++
 Supports APIs (drivers) in many computer languages
 JavaScript, Python, Ruby, Perl, Java, Java Scala, C#, C++, Haskell, Erlang
Functionality of MongoDB
 Dynamic schema
 No DDL
 Document-based database
 Secondary indexes
 Query language via an API
 Atomic writes and fully-consistent reads
 If system configured that way
 Master-slave replication with automated failover (replica sets)
 Built-in horizontal scaling via automated range-based partitioning of data
(sharding)
 No joins nor transactions
Why use MongoDB?
 Simple queries
 Functionality provided applicable to most web applications
 Easy and fast integration of data
 No ERD diagram
 Not well suited for heavy and complex transactions systems
MongoDB: CAP approach
 Focus on Consistency and Partition tolerance
 Consistency
 all replicas contain the same version of the data
 Availability
 system remains operational on failing nodes • Partition tolarence
 multiple entry points
 system remains operational on system split
MongoDB: Hierarchical Objects
• A MongoDB instance may have zero or more ‘databases’
• A database may have zero or more ‘collections’.
• A collection may have zero or more ‘documents’.
• A document may have one or more ‘fields’.
• MongoDB ‘Indexes’ function much like their RDBMS counterparts.
MongoDB Processes and configuration
 Mongod – Database instance
 Mongos - Sharding processes
 Analogous to a database router.
 Processes all requests
 Decides how many and which mongodsshould receive the query
 Mongos collates the results, and sends it back to the client.
 Mongo – an interactive shell ( a client)
 Fully functional JavaScript environment for use with a MongoDB
 You can have one mongos for the whole system no matter how many
mongods you have
Choices made for Design of MongoDB
 Scale horizontally over commodity hardware
 Lots of relatively inexpensive servers
 Keep the functionality that works well in RDBMSs – Ad hoc queries –
Fully featured indexes – Secondary indexes
 What doesn’t distribute well in RDB?
– Long running multi-row transactions
– Joins
– Both artifacts of the relational data model (row x column)
BSON format
 Binary-encoded serialization of JSON-like documents
 Zero or more key/value pairs are stored as a single entity
 Each entry consists of a field name, a data type, and a value
 Large elements in a BSON document are prefixed with a length field to
facilitate scanning
JSON format
 Data is in name / value pairs
 A name/value pair consists of a field name followed by a colon, followed
by a value: • Example: “name”: “R2-D2”
 Data is separated by commas
Example: “name”: “R2-D2”, race : “Droid” • Curly braces hold
objects
Example: {“name”: “R2-D2”, race : “Droid”, affiliation: “rebels”}
 An array is stored in brackets []
Example [ {“name”: “R2-D2”, race : “Droid”, affiliation: “rebels”},
 {“name”: “Yoda”, affiliation: “rebels”} ]
MongoDB Features
• Document-Oriented storage
• Full Index Support
• Replication & High Availability
• Auto-Sharding
• Querying
• Fast In-Place Updates
• Map/Reduce functionality
Index Functionality
 B+ tree indexes
 An index is automatically created on the _id field (the
primary key)
 Users can create other indexes to improve query performance
or to enforce Unique values for a particular field
 Supports single field index as well as Compound index
 Like SQL order of the fields in a compound index matters
 If you index a field that holds an array value, MongoDB
creates separate index entries for every element of the array.
Cont..
• Sparse property of an index ensures that the index only contain entries for
documents that have the indexed field. (so ignore records that do not have
the field defined)
• If an index is both unique and sparse – then the system will reject records
that have a duplicate key value but allow records that do not have the
indexed field defined
CRUD operations
• Create
db.collection.insert( )
db.collection.save( )
db.collection.update( , , { upsert: true } )
• Read
db.collection.find( , )
db.collection.findOne( , )
• Update
db.collection.update( , , )
• Delete
db.collection.remove( , )
Aggregated functionality
 Aggregation framework provides SQL-like aggregation functionality
 Pipeline documents from a collection pass through an aggregation
pipeline, which transforms these objects as they pass through
 Expressions produce output documents based on calculations performed on
input documents
 Example db.parts.aggregate ( {$group : {_id: type, totalquantity : { $sum:
quanity} } } )
Map reduce functionality
 Performs complex aggregator functions given a collection of keys, value
pairs
 Must provide at least a map function, reduction function and a name of the
result set
 db.collection.mapReduce( , , { out: , query: , sort: , limit: , finalize: , scope:
, jsMode: , verbose: } )
Indexes: High performance read
 Typically used for frequently used queries
 Necessary when the total size of the documents exceeds the amount of
available RAM.
 Defined on the collection level
 Can be defined on 1 or more fields
 Composite index (SQL) Compound index (MongoDB)

 B-tree index
 Only 1 index can be used by the query optimizer when retrieving data
Replication of data
 Ensures redundancy, backup, and automatic failover
 Recovery manager in the RDMS
 Replication occurs through groups of servers known as replica sets
 Primary set – set of servers that client tasks direct updates to
 Secondary set – set of servers used for duplication of data
Consistency of data
 All read operations issued to the primary of a replica set are consistent
with the last write operation
 Reads to a primary have strict consistency
 Reads reflect the latest changes to the data Reads to a secondary have
eventual consistency
 Updates propagate gradually
Ad

More Related Content

Similar to MongoDB is a document database. It stores data in a type of JSON format called BSON. (20)

Mongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorialMongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorial
Mohan Rathour
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
S.Shayan Daneshvar
 
MongoDB @ fliptop
MongoDB @ fliptopMongoDB @ fliptop
MongoDB @ fliptop
Robbie Cheng
 
MongoDB 2.4 and spring data
MongoDB 2.4 and spring dataMongoDB 2.4 and spring data
MongoDB 2.4 and spring data
Jimmy Ray
 
MongoDB
MongoDBMongoDB
MongoDB
Bembeng Arifin
 
Elasticsearch vs MongoDB comparison
Elasticsearch vs MongoDB comparisonElasticsearch vs MongoDB comparison
Elasticsearch vs MongoDB comparison
jeetendra mandal
 
Scalany mongodb aug10
Scalany mongodb aug10Scalany mongodb aug10
Scalany mongodb aug10
bwmcadams
 
Introduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesIntroduction to MongoDB and its best practices
Introduction to MongoDB and its best practices
AshishRathore72
 
Node Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js TutorialNode Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js Tutorial
PHP Support
 
UNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptxUNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptx
DharaDarji5
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
HabileLabs
 
DBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training PresentationsDBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training Presentations
Srinivas Mutyala
 
MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...
Ram Murat Sharma
 
Data Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA 2022 - What's new with MongoDB 6.0 and AtlasData Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA
 
Big data technology unit 3
Big data technology unit 3Big data technology unit 3
Big data technology unit 3
RojaT4
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB Internals
Siraj Memon
 
Mongodb - NoSql Database
Mongodb - NoSql DatabaseMongodb - NoSql Database
Mongodb - NoSql Database
Prashant Gupta
 
Analytical data processing
Analytical data processingAnalytical data processing
Analytical data processing
Polad Saruxanov
 
Power Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DB
Power Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DBPower Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DB
Power Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DB
PowerSaturdayParis
 
Gluecon 2012 - DynamoDB
Gluecon 2012 - DynamoDBGluecon 2012 - DynamoDB
Gluecon 2012 - DynamoDB
Jeff Douglas
 
Mongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorialMongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorial
Mohan Rathour
 
MongoDB 2.4 and spring data
MongoDB 2.4 and spring dataMongoDB 2.4 and spring data
MongoDB 2.4 and spring data
Jimmy Ray
 
Elasticsearch vs MongoDB comparison
Elasticsearch vs MongoDB comparisonElasticsearch vs MongoDB comparison
Elasticsearch vs MongoDB comparison
jeetendra mandal
 
Scalany mongodb aug10
Scalany mongodb aug10Scalany mongodb aug10
Scalany mongodb aug10
bwmcadams
 
Introduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesIntroduction to MongoDB and its best practices
Introduction to MongoDB and its best practices
AshishRathore72
 
Node Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js TutorialNode Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js Tutorial
PHP Support
 
UNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptxUNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptx
DharaDarji5
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
HabileLabs
 
DBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training PresentationsDBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training Presentations
Srinivas Mutyala
 
MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...
Ram Murat Sharma
 
Data Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA 2022 - What's new with MongoDB 6.0 and AtlasData Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA
 
Big data technology unit 3
Big data technology unit 3Big data technology unit 3
Big data technology unit 3
RojaT4
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB Internals
Siraj Memon
 
Mongodb - NoSql Database
Mongodb - NoSql DatabaseMongodb - NoSql Database
Mongodb - NoSql Database
Prashant Gupta
 
Analytical data processing
Analytical data processingAnalytical data processing
Analytical data processing
Polad Saruxanov
 
Power Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DB
Power Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DBPower Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DB
Power Saturday 2019 B4 - From relational to Multimodel Azure Cosmos DB
PowerSaturdayParis
 
Gluecon 2012 - DynamoDB
Gluecon 2012 - DynamoDBGluecon 2012 - DynamoDB
Gluecon 2012 - DynamoDB
Jeff Douglas
 

Recently uploaded (20)

How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Ad

MongoDB is a document database. It stores data in a type of JSON format called BSON.

  • 2. What is MongoDB?  Developed by 10gen  Founded in 2007  A document-oriented, NoSQL database  Hash-based, schema-less database • No Data Definition Language  In practice, this means you can store hashes with any keys and values that you choose • Keys are a basic data type but in reality stored as strings  Document Identifiers (_id) will be created for each document, field name reserved by system
  • 3. Cont..  Application tracks the schema and mapping • Uses BSON format  Based on JSON  Written in C++  Supports APIs (drivers) in many computer languages  JavaScript, Python, Ruby, Perl, Java, Java Scala, C#, C++, Haskell, Erlang
  • 4. Functionality of MongoDB  Dynamic schema  No DDL  Document-based database  Secondary indexes  Query language via an API  Atomic writes and fully-consistent reads  If system configured that way  Master-slave replication with automated failover (replica sets)  Built-in horizontal scaling via automated range-based partitioning of data (sharding)  No joins nor transactions
  • 5. Why use MongoDB?  Simple queries  Functionality provided applicable to most web applications  Easy and fast integration of data  No ERD diagram  Not well suited for heavy and complex transactions systems
  • 6. MongoDB: CAP approach  Focus on Consistency and Partition tolerance  Consistency  all replicas contain the same version of the data  Availability  system remains operational on failing nodes • Partition tolarence  multiple entry points  system remains operational on system split
  • 7. MongoDB: Hierarchical Objects • A MongoDB instance may have zero or more ‘databases’ • A database may have zero or more ‘collections’. • A collection may have zero or more ‘documents’. • A document may have one or more ‘fields’. • MongoDB ‘Indexes’ function much like their RDBMS counterparts.
  • 8. MongoDB Processes and configuration  Mongod – Database instance  Mongos - Sharding processes  Analogous to a database router.  Processes all requests  Decides how many and which mongodsshould receive the query  Mongos collates the results, and sends it back to the client.  Mongo – an interactive shell ( a client)  Fully functional JavaScript environment for use with a MongoDB  You can have one mongos for the whole system no matter how many mongods you have
  • 9. Choices made for Design of MongoDB  Scale horizontally over commodity hardware  Lots of relatively inexpensive servers  Keep the functionality that works well in RDBMSs – Ad hoc queries – Fully featured indexes – Secondary indexes  What doesn’t distribute well in RDB? – Long running multi-row transactions – Joins – Both artifacts of the relational data model (row x column)
  • 10. BSON format  Binary-encoded serialization of JSON-like documents  Zero or more key/value pairs are stored as a single entity  Each entry consists of a field name, a data type, and a value  Large elements in a BSON document are prefixed with a length field to facilitate scanning
  • 11. JSON format  Data is in name / value pairs  A name/value pair consists of a field name followed by a colon, followed by a value: • Example: “name”: “R2-D2”  Data is separated by commas Example: “name”: “R2-D2”, race : “Droid” • Curly braces hold objects Example: {“name”: “R2-D2”, race : “Droid”, affiliation: “rebels”}  An array is stored in brackets [] Example [ {“name”: “R2-D2”, race : “Droid”, affiliation: “rebels”},  {“name”: “Yoda”, affiliation: “rebels”} ]
  • 12. MongoDB Features • Document-Oriented storage • Full Index Support • Replication & High Availability • Auto-Sharding • Querying • Fast In-Place Updates • Map/Reduce functionality
  • 13. Index Functionality  B+ tree indexes  An index is automatically created on the _id field (the primary key)  Users can create other indexes to improve query performance or to enforce Unique values for a particular field  Supports single field index as well as Compound index  Like SQL order of the fields in a compound index matters  If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array.
  • 14. Cont.. • Sparse property of an index ensures that the index only contain entries for documents that have the indexed field. (so ignore records that do not have the field defined) • If an index is both unique and sparse – then the system will reject records that have a duplicate key value but allow records that do not have the indexed field defined
  • 15. CRUD operations • Create db.collection.insert( ) db.collection.save( ) db.collection.update( , , { upsert: true } ) • Read db.collection.find( , ) db.collection.findOne( , ) • Update db.collection.update( , , ) • Delete db.collection.remove( , )
  • 16. Aggregated functionality  Aggregation framework provides SQL-like aggregation functionality  Pipeline documents from a collection pass through an aggregation pipeline, which transforms these objects as they pass through  Expressions produce output documents based on calculations performed on input documents  Example db.parts.aggregate ( {$group : {_id: type, totalquantity : { $sum: quanity} } } )
  • 17. Map reduce functionality  Performs complex aggregator functions given a collection of keys, value pairs  Must provide at least a map function, reduction function and a name of the result set  db.collection.mapReduce( , , { out: , query: , sort: , limit: , finalize: , scope: , jsMode: , verbose: } )
  • 18. Indexes: High performance read  Typically used for frequently used queries  Necessary when the total size of the documents exceeds the amount of available RAM.  Defined on the collection level  Can be defined on 1 or more fields  Composite index (SQL) Compound index (MongoDB)   B-tree index  Only 1 index can be used by the query optimizer when retrieving data
  • 19. Replication of data  Ensures redundancy, backup, and automatic failover  Recovery manager in the RDMS  Replication occurs through groups of servers known as replica sets  Primary set – set of servers that client tasks direct updates to  Secondary set – set of servers used for duplication of data
  • 20. Consistency of data  All read operations issued to the primary of a replica set are consistent with the last write operation  Reads to a primary have strict consistency  Reads reflect the latest changes to the data Reads to a secondary have eventual consistency  Updates propagate gradually
  翻译: