SlideShare a Scribd company logo
1 of 30
What is MongoDB?
“Humongous” DB
 Lots of similarities with SQL RDBMs, but with more flexibility
 No joins because data is denormalized & pre-joined
 Open source
RDBMS MongoDB
Database ➜ Database
Table / View ➜ Collection
Row / Tuple ➜ Document
Index ➜ Index
Join ➜ Embedded Document
PK: Any
Attribute(s)
➜ PK: _id Field
Foreign Key ➜ Reference
Storage model
Emphasize data locality:
 Each document stored as one unit, even with embedding
 Each collection stored as one unit.
Uses BSON, a binary-encoded variant of JSON key/value pairs
JSON
“JavaScript Object Notation”
Easy for humans to write/read, easy for computers to parse/generate
Objects can be nested
Built on
name/value pairs
Ordered list of values
http://json.org/
BSON
• “Binary JSON”
• Binary-encoded serialization of JSON-like docs
• Also allows “referencing”
• Embedded structure reduces need for joins
• Goals
– Lightweight
– Traversable
– Efficient (decoding and encoding)
http://bsonspec.org/
BSON Example
{
"_id" : "37010"
"city" : “MDU",
"pop" : 2660,
"state" : "TN",
“minister” : {
name: “XX”
address: “XYZ”
}
}
The _id Field
• By default, each document contains an _id field. This field has a
number of special characteristics:
– Value serves as primary key for collection.
– Value is unique, immutable, and may be any non-array type.
– Default data type is ObjectId, which is “small, likely unique, fast to
generate, and ordered.”
– Sorting on an ObjectId value is roughly equivalent to sorting on creation
time.
Collections of documents
{
_id: “S1234”,
first_name: “John”,
last_name: “Smith”,
major: “ECON”
}
{
_id: “S5678”,
first_name: “Mary”,
last_name: “Jones”,
major: “COMP”,
minor: “STAT”
}
Collection = table
Document = record
Every document
has unique _id,
even if not user-
defined.
Documents need
not have the same
fields.
SQL-like, but with a library
Create Operations
inserting details of a single student in the form of document in the student
collection using db.collection.insertOne() method
Create Operations
inserting details of the multiple
students in the form of
documents in the student
collection
using db.collection.insertMany() metho
d.
Relating data via embedding
{
_id: “S1234”,
first_name: “John”,
last_name: “Smith”,
major: “ECON”,
address: {
street: “1514 Nowhere St.”,
city: “Houston”,
state: “TX”
}
}
Data all stored
together for locality.
Denormalized.
Possible
redundancy.
Relating data via embedding
{
_id: “S1234”,
first_name: “John”,
last_name: “Smith”,
major: “ECON”,
addresses: [{
street: “1514 Nowhere St.”,
city: “Houston”,
state: “TX”},
{street: “7729 Somewhere St.”,
city: “Kansas City”,
state: “KS”}
]
}
Relating data via references
/////author
{
_id: 34237
first_name: “John”,
last_name: “Smith”
}
/////books
{
title: “A book about books”,
author: 34237
}
{
title: “A little bookish book”,
author: 34237
}
{
title: “A new book”,
author: 34237
}
_id field = primary key
Reference = foreign key
Relating data via references
{
first_name: “John”,
last_name: “Smith”,
books: [103, 279, 541]
}
{
_id: 103,
title: “A book about books”
}
{
_id: 279,
title: “A little bookish book”
}
{
_id: 541,
title: “A new book”
}
_id field = primary key
Reference = foreign key
Embedding & relating replaces joins
No need for joins, because data is already explicitly linked.
This gives up SQL’s flexibility of joining on conditions other than PK=FK.
Focuses on the common case.
Read Operation - Filtering
db.students.find({major: “COMP”})
db.students.find({major: “COMP”, minor: “STAT”})
db.students.find({$or: [{major: “COMP”}, {major: “ELEC”}]})
db.students.find({major: {$in: [“COMP”, “ELEC”]}})
db.students.find({gpa: {$gt: 3.8}})
db.students.find({address.street: {$exists: true}})
Collection
name
Syntax structured as
key/value pairs with
some special $keywords.
Embedded
field name
Selecting fields
db.students.find({major: “COMP”}, {first_name: true, last_name: true, major: false})
Filter
Specifies desired
fields.
db.students.insert({first_name: “Susan”, last_name: “Walsh”})
db.students.update({first_name: “John”, last_name: “Smith”},
{$set: {major: “HIST”}},
{multi: true,
upsert: true})
db.students.update({first_name: “John”, last_name: “Smith”},
{first_name: “Jonathan”, last_name: “Smythe”})
db.students.update({major: “COMP”},
{$unset: {minor: “”}})
Filter
Change multiple
documents?
Insert if no match
found?
Delete field.
Change entire
document.
Update Operations
Change only this.
Delete Operations
db.students.remove({major: “COMP”})
Remove all records where major is COMP.
Transactions
db.students.update(…, $isolated: true)
db.students.remove(…, $isolated: true)
By default, each write to a document is
atomic.
Make entire
collections’ writes
atomic.
Not intended for transaction-heavy systems.
Grouping & aggregation
db.students.aggregate({$match: …},
{$group: …},
{$sort: …})
db.students.mapReduce(map_fn, reduce_fn)
db.students.distinct(“major”)
Aggregation
Map-Reduce
Has two phases:
 A map stage that processes each document and emits one or more objects for
each input document
 A reduce phase that combines the output of the map operation.
 An optional finalize stage for final modifications to the result
Uses Custom JavaScript functions
Provides greater flexibility but is less efficient and more complex
than the aggregation pipeline
Can have output sets that exceed the 16 megabyte output limitation of
the aggregation pipeline.
Distinct
B-tree indices
db.students.ensureIndex({last_name: 1, gpa: -1})
Creates index that first orders last_name
ascending, then gpa descending.
Automatically create index on
_id.
THANK YOU

More Related Content

Similar to MongoDB - Features and Operations

Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Andrii Lashchenko
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseMarco Gralike
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)Faysal Shaarani (MBA)
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation FrameworkCaserta
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDBKishor Parkhe
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolatorMichael Limansky
 
Introduction to MongoDB and Workshop
Introduction to MongoDB and WorkshopIntroduction to MongoDB and Workshop
Introduction to MongoDB and WorkshopAhmedabadJavaMeetup
 
Mongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMetatagg Solutions
 
MongoDB Knowledge share
MongoDB Knowledge shareMongoDB Knowledge share
MongoDB Knowledge shareMr Kyaing
 
Elasticsearch for SQL Users
Elasticsearch for SQL UsersElasticsearch for SQL Users
Elasticsearch for SQL UsersAll Things Open
 
MongoDB Aggregations Indexing and Profiling
MongoDB Aggregations Indexing and ProfilingMongoDB Aggregations Indexing and Profiling
MongoDB Aggregations Indexing and ProfilingManish Kapoor
 

Similar to MongoDB - Features and Operations (20)

Polyglot Persistence
Polyglot PersistencePolyglot Persistence
Polyglot Persistence
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory Database
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Mongo db
Mongo dbMongo db
Mongo db
 
JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Json
JsonJson
Json
 
Mongo DB 102
Mongo DB 102Mongo DB 102
Mongo DB 102
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDB
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolator
 
Elasticsearch
ElasticsearchElasticsearch
Elasticsearch
 
Introduction to MongoDB and Workshop
Introduction to MongoDB and WorkshopIntroduction to MongoDB and Workshop
Introduction to MongoDB and Workshop
 
Mongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg Solutions
 
MongoDB Knowledge share
MongoDB Knowledge shareMongoDB Knowledge share
MongoDB Knowledge share
 
Elasticsearch for SQL Users
Elasticsearch for SQL UsersElasticsearch for SQL Users
Elasticsearch for SQL Users
 
Working with JSON
Working with JSONWorking with JSON
Working with JSON
 
Json
JsonJson
Json
 
MongoDB Aggregations Indexing and Profiling
MongoDB Aggregations Indexing and ProfilingMongoDB Aggregations Indexing and Profiling
MongoDB Aggregations Indexing and Profiling
 

More from ramyaranjith

Tools for Project Excellence.pptx
Tools for Project Excellence.pptxTools for Project Excellence.pptx
Tools for Project Excellence.pptxramyaranjith
 
Variadic functions
Variadic functionsVariadic functions
Variadic functionsramyaranjith
 
Command line arguments
Command line argumentsCommand line arguments
Command line argumentsramyaranjith
 
CS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved ExamplesCS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved Examplesramyaranjith
 

More from ramyaranjith (8)

Tools for Project Excellence.pptx
Tools for Project Excellence.pptxTools for Project Excellence.pptx
Tools for Project Excellence.pptx
 
ML with IoT
ML with IoTML with IoT
ML with IoT
 
Regression
RegressionRegression
Regression
 
Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Bit fields
Bit fieldsBit fields
Bit fields
 
Unified Process
Unified Process Unified Process
Unified Process
 
CS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved ExamplesCS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved Examples
 

Recently uploaded

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 

Recently uploaded (20)

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 

MongoDB - Features and Operations

  • 1.
  • 2. What is MongoDB? “Humongous” DB  Lots of similarities with SQL RDBMs, but with more flexibility  No joins because data is denormalized & pre-joined  Open source
  • 3. RDBMS MongoDB Database ➜ Database Table / View ➜ Collection Row / Tuple ➜ Document Index ➜ Index Join ➜ Embedded Document PK: Any Attribute(s) ➜ PK: _id Field Foreign Key ➜ Reference
  • 4. Storage model Emphasize data locality:  Each document stored as one unit, even with embedding  Each collection stored as one unit. Uses BSON, a binary-encoded variant of JSON key/value pairs
  • 5. JSON “JavaScript Object Notation” Easy for humans to write/read, easy for computers to parse/generate Objects can be nested Built on name/value pairs Ordered list of values http://json.org/
  • 6. BSON • “Binary JSON” • Binary-encoded serialization of JSON-like docs • Also allows “referencing” • Embedded structure reduces need for joins • Goals – Lightweight – Traversable – Efficient (decoding and encoding) http://bsonspec.org/
  • 7. BSON Example { "_id" : "37010" "city" : “MDU", "pop" : 2660, "state" : "TN", “minister” : { name: “XX” address: “XYZ” } }
  • 8. The _id Field • By default, each document contains an _id field. This field has a number of special characteristics: – Value serves as primary key for collection. – Value is unique, immutable, and may be any non-array type. – Default data type is ObjectId, which is “small, likely unique, fast to generate, and ordered.” – Sorting on an ObjectId value is roughly equivalent to sorting on creation time.
  • 9. Collections of documents { _id: “S1234”, first_name: “John”, last_name: “Smith”, major: “ECON” } { _id: “S5678”, first_name: “Mary”, last_name: “Jones”, major: “COMP”, minor: “STAT” } Collection = table Document = record Every document has unique _id, even if not user- defined. Documents need not have the same fields.
  • 10. SQL-like, but with a library
  • 11. Create Operations inserting details of a single student in the form of document in the student collection using db.collection.insertOne() method
  • 12. Create Operations inserting details of the multiple students in the form of documents in the student collection using db.collection.insertMany() metho d.
  • 13. Relating data via embedding { _id: “S1234”, first_name: “John”, last_name: “Smith”, major: “ECON”, address: { street: “1514 Nowhere St.”, city: “Houston”, state: “TX” } } Data all stored together for locality. Denormalized. Possible redundancy.
  • 14. Relating data via embedding { _id: “S1234”, first_name: “John”, last_name: “Smith”, major: “ECON”, addresses: [{ street: “1514 Nowhere St.”, city: “Houston”, state: “TX”}, {street: “7729 Somewhere St.”, city: “Kansas City”, state: “KS”} ] }
  • 15. Relating data via references /////author { _id: 34237 first_name: “John”, last_name: “Smith” } /////books { title: “A book about books”, author: 34237 } { title: “A little bookish book”, author: 34237 } { title: “A new book”, author: 34237 } _id field = primary key Reference = foreign key
  • 16. Relating data via references { first_name: “John”, last_name: “Smith”, books: [103, 279, 541] } { _id: 103, title: “A book about books” } { _id: 279, title: “A little bookish book” } { _id: 541, title: “A new book” } _id field = primary key Reference = foreign key
  • 17. Embedding & relating replaces joins No need for joins, because data is already explicitly linked. This gives up SQL’s flexibility of joining on conditions other than PK=FK. Focuses on the common case.
  • 18. Read Operation - Filtering db.students.find({major: “COMP”}) db.students.find({major: “COMP”, minor: “STAT”}) db.students.find({$or: [{major: “COMP”}, {major: “ELEC”}]}) db.students.find({major: {$in: [“COMP”, “ELEC”]}}) db.students.find({gpa: {$gt: 3.8}}) db.students.find({address.street: {$exists: true}}) Collection name Syntax structured as key/value pairs with some special $keywords. Embedded field name
  • 19. Selecting fields db.students.find({major: “COMP”}, {first_name: true, last_name: true, major: false}) Filter Specifies desired fields.
  • 20. db.students.insert({first_name: “Susan”, last_name: “Walsh”}) db.students.update({first_name: “John”, last_name: “Smith”}, {$set: {major: “HIST”}}, {multi: true, upsert: true}) db.students.update({first_name: “John”, last_name: “Smith”}, {first_name: “Jonathan”, last_name: “Smythe”}) db.students.update({major: “COMP”}, {$unset: {minor: “”}}) Filter Change multiple documents? Insert if no match found? Delete field. Change entire document. Update Operations Change only this.
  • 22. Transactions db.students.update(…, $isolated: true) db.students.remove(…, $isolated: true) By default, each write to a document is atomic. Make entire collections’ writes atomic. Not intended for transaction-heavy systems.
  • 23.
  • 24. Grouping & aggregation db.students.aggregate({$match: …}, {$group: …}, {$sort: …}) db.students.mapReduce(map_fn, reduce_fn) db.students.distinct(“major”)
  • 26. Map-Reduce Has two phases:  A map stage that processes each document and emits one or more objects for each input document  A reduce phase that combines the output of the map operation.  An optional finalize stage for final modifications to the result Uses Custom JavaScript functions Provides greater flexibility but is less efficient and more complex than the aggregation pipeline Can have output sets that exceed the 16 megabyte output limitation of the aggregation pipeline.
  • 27.
  • 29. B-tree indices db.students.ensureIndex({last_name: 1, gpa: -1}) Creates index that first orders last_name ascending, then gpa descending. Automatically create index on _id.