SlideShare a Scribd company logo
An Introduction to
MongoDB
Anuj Jain
Equal Experts India
MongoDB
NoSQL

Key-value

Graph database

Document-oriented

Column family
The Great Divide
Not a RDBMS
• Mongo is not a relational database like MySQL, Oracle
• No transactions.
• No referential Integrity.
• No Joins.
• No schema, so no columns or rows.
• NoSQL.
What is MongoDB ?
• Scalable High-Performance Open-source,
Document-orientated database.
• Built for Speed
• Rich Document based queries for Easy readability.
• Full Index Support for High Performance.
• Replication and Failover for High Availability.
• Auto Sharding for Easy Scalability.
• Map / Reduce for Aggregation
Quiz ?
Which of the following statement are true about MongoDB ?
1. MongoDB is document oriented.
2. MongoDB supports Joins.
3. MongoDB has dynamic schema.
4. MongoDB supports SQL.
What MongoDB is great for ?
• Semi structured Content Management.
• Real time Analytics and High-Speed Logging.
• Caching and High Availability.
• Mobile and Social Infrastructure, Big Data etc.
Some considerations while designing
schema in MongoDB
• Combine objects into one document if you will use them together.
Otherwise separate them (but make sure there should not be
need of joins).
• Duplicate the data (but limited) because disk space is cheap as
compare to compute time.
• Do joins while write, not on read.
• Optimize your schema for most frequent use cases.
• Do complex aggregation in the schema.
Example – Blog Post
• Every post has the unique title, description and url.
• Every post can have one or more tags.
• Every post has the name of its publisher and total number of
likes.
• Every Post have comments given by users along with their name,
message, data-time and likes.
• On each post there can be zero or more comments.
RDBMS
Mongo Schema
{
“_id” : ObjectId("55b1f50899708bec87f96edc")
“title” : “MongoDB Tutorial for beginner”,
“description: “How to start using mongodb”,
“by: Anuj Jain,
“url: “http://mongodbtutorial.com/blog/mongodb”,
“tags” : ['mongodb', 'nosql' ],
“likes” : 200,
“comments” : [
{
“user” : ''MongoUser”,
“message” : “Very Nice Tutorial” ,
“dateCreated” : NumberLong(1437725960469),
“like” : true
}
]
}
Quiz ?
How many different data types are there in JSON ?
1. 4
2. 5
3. 6
4. 7
Answer
Ans: 6
1. String
2. Number
3. Boolean
4. null
5. Array
6. Object/document
CRUD
Create
db.collection.insert( <document> )
db.collection.save( <document> )
db.collection.update( <query>, <update>, { upsert: true } )
Read
db.collection.find( <query>, <projection> )
db.collection.findOne( <query>, <projection> )
Update
db.collection.update( <query>, <update>, <options> )
Delete
db.collection.remove( <query>, <justOne> )
Some Other Operators
1. $and
2. $or
3. $in
4. $nin
5. $exists
6. $push
7. $pop
8. $addToSet
Indexes
1.Indexes are special data structures, that store a small portion of
the data set in an easy to traverse form.
2. Stores the value of a specific field or set of fields.
3. Ordered by the value of the field as specified in index.
4. Indexes can improves read operation but slower the write
operations.
5. Mongodb use B-Tree data structure to store indexes.
6.Blocks mongod process unless you specify the background.
7. null is assumed if field is missing.
When To Index ?
1. Frequently Queried Fields
2. Low response time
3. Sorting
4. Avoid full collection scans.
Indexes Types
1. Default (_id)
2. Single Field
3. Compound Index
4. Multikey Index
5. Geospatial Index
6. Sparse Index
7. TTL Index
Quiz ?
Mongodb index can have keys of different types
( ints, dates, string for example) in it ?
1. True
2. False
Covered Indexes
Queries that can be resolved with only the index (does not need to
fetch the original document)
Example: { “name”:”Anuj”,
– “age”:28,
– “gender”:Male,
– “skills”:[“Java”,”Mongo”]
}
db.people.ensureIndex({“name”:1,”age”:1})
db.people.find ({“name”:”Anuj”},{“_id” :0 , “age”:1})
TTL Indexes {“time to live”}
1. Mongod already remove the data from the collections after specify
number of seconds.
2. Field type should either be BSON date type or an array of BSON date-
type object
Eg. db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds:
3600 } )
Where createdAt is date field
Quiz ?
Suppose we run :
db.foo.ensureIndex ({a:1, b:2, c:3})
db.foo.find({a : “sports”, b:{$gt : 100}})
Then
1.Only the index needs touched to fully execute the
query.
2.Then index and some documents need to be executed.
Why Replication?
• To keep your data safe.
• High (24*7) availability of data.
• Disaster Recovery.
• Read scaling (extra copies to read from).
• No downtime for maintenance (like backups, index
rebuilds, compaction).
Replica set features
• A class of N nodes.
• Anyone node can be primary.
• All write operation goes to primary.
• Automatic fail-overs.
• Automatic Recovery.
• Consensus election of primary.
Capped Collection
• Fixed size circular queues that follow the insertion order.
• Fixed size is preallocated and when it exhausted, oldest
document will automatically start getting deleted.
• We cannot delete documents from a capped collection.
• There are no default indexes present in a capped collection, not
even on _id field.
Capped Collection
Commands :
1. db.createCollection (
"cappedcollection", {capped:true,size:10000} )
2. db.createCollection (
"cappedcollection", capped:true, size:10000, max:1000 })
3. db.cappedLogCollection.isCapped()
4. db.runCommand({"convertToCapped":"posts",size:10000})
5. db.cappedLogCollection.find().sort({$natural:-1})
Query Limitations:
Indexing can't be used in queries which use:
1. Regular expressions or negation operators like $nin, $not, etc.
Arithmetic operators like $mod, etc.
2. $where clause
Hence, it is always advisable to check the index usage for your
queries.
Index Limitation
Maximum Ranges:
• A collection cannot have more than 64 indexes.
• The length of the index name cannot be longer than 125
characters.
• A compound index can have maximum 31 fields indexed
$explain
The $explain operator provides information and statistics on the
query for example :
1. Indexes used the query.
2. Number of document scan in serving the query.
3. Whether index enough to serve the query data i.e. covered
Index.
Usage :
db.users.find({gender:"M"}, {user_name:1,_id:0} ).explain()
$hint
The $hint operator forces the query optimizer to use the specified
index to run a query
db.users.find({gender:"M"},{user_name:1,_id:0})
» .hint({gender:1,user_name:1})
Backup & Restore
Backup Utilities
1. mongodump (use to dump complete data directory or db)
2. mongoexport (use to dump certain collection to output file like
json or csv).
Restore Utilities
1. mongorestore
2. mongoimport
ObjectId
An ObjectId is a 12-byte BSON type having the following
structure:
The first 4 bytes representing the seconds since the unix epoch
The next 3 bytes are the machine identifier
The next 2 bytes consists of process id
The last 3 bytes are a random counter value
Thank you
References:
https://www.mongodb.org/

More Related Content

PPTX
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
PPTX
Socialite, the Open Source Status Feed
PPTX
Building a Scalable Inbox System with MongoDB and Java
PPTX
Socialite, the Open Source Status Feed Part 2: Managing the Social Graph
KEY
Schema Design by Example ~ MongoSF 2012
PPTX
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...
PPT
Building web applications with mongo db presentation
KEY
Schema Design with MongoDB
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed
Building a Scalable Inbox System with MongoDB and Java
Socialite, the Open Source Status Feed Part 2: Managing the Social Graph
Schema Design by Example ~ MongoSF 2012
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...
Building web applications with mongo db presentation
Schema Design with MongoDB

What's hot (20)

PPTX
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
PPTX
MongoDB Schema Design: Four Real-World Examples
PPTX
MongoDB Advanced Schema Design - Inboxes
PDF
MongoDB Schema Design
PPTX
Indexing Strategies to Help You Scale
PDF
Building your first app with mongo db
PPT
Building Your First MongoDB App ~ Metadata Catalog
PDF
Mongo DB schema design patterns
PPTX
Introduction to MongoDB
PPTX
Data Modeling for the Real World
PPTX
Webinar: Back to Basics: Thinking in Documents
PPT
MongoDB Schema Design
PPTX
Webinar: Schema Design
PDF
Building your first app with MongoDB
PDF
Building a Social Network with MongoDB
PPTX
Back to Basics Webinar 3: Schema Design Thinking in Documents
PPTX
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...
PPT
5 Pitfalls to Avoid with MongoDB
PPTX
Dev Jumpstart: Schema Design Best Practices
PPTX
MongoDB Schema Design: Practical Applications and Implications
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB Schema Design: Four Real-World Examples
MongoDB Advanced Schema Design - Inboxes
MongoDB Schema Design
Indexing Strategies to Help You Scale
Building your first app with mongo db
Building Your First MongoDB App ~ Metadata Catalog
Mongo DB schema design patterns
Introduction to MongoDB
Data Modeling for the Real World
Webinar: Back to Basics: Thinking in Documents
MongoDB Schema Design
Webinar: Schema Design
Building your first app with MongoDB
Building a Social Network with MongoDB
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB London 2013: Data Modeling Examples from the Real World presented by ...
5 Pitfalls to Avoid with MongoDB
Dev Jumpstart: Schema Design Best Practices
MongoDB Schema Design: Practical Applications and Implications
Ad

Viewers also liked (20)

KEY
Administration (Eliot Horowitz)
PPTX
MediaGlu and Mongo DB
PPTX
Shankar's mongo db presentation
PDF
Mongo db basics
PPT
Mongo db basics
PPTX
Connecting NodeJS & MongoDB
PDF
NOSQL Overview
PPTX
NoSQL databases - An introduction
PPTX
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
PDF
Mongo db
ZIP
NoSQL databases
PPTX
MongoDB for Beginners
PPTX
Mongo DB
PPTX
Mongo db
PPTX
IOT Based Home Automation using Raspberry Pi-3
PDF
Mongo DB
PPTX
MongoDB-Beginner tutorial explaining basic operation via mongo shell
PDF
Intro To MongoDB
PPT
Introduction to MongoDB
Administration (Eliot Horowitz)
MediaGlu and Mongo DB
Shankar's mongo db presentation
Mongo db basics
Mongo db basics
Connecting NodeJS & MongoDB
NOSQL Overview
NoSQL databases - An introduction
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo db
NoSQL databases
MongoDB for Beginners
Mongo DB
Mongo db
IOT Based Home Automation using Raspberry Pi-3
Mongo DB
MongoDB-Beginner tutorial explaining basic operation via mongo shell
Intro To MongoDB
Introduction to MongoDB
Ad

Similar to Mongo db tutorials (20)

PPT
Fast querying indexing for performance (4)
PDF
Nosql part 2
PPTX
PDF
Mongo db eveningschemadesign
PDF
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
PPT
MongoDB Pros and Cons
PPTX
MongoDB using Grails plugin by puneet behl
KEY
2012 phoenix mug
PPTX
PDF
MongoDB and Ruby on Rails
PDF
Mongodb Introduction
PDF
Webinar: Was ist neu in MongoDB 2.4
PDF
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
PDF
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
PPTX
Mongo db
PPTX
Introduction to MongoDB
PDF
Intresting changes in mongo 2.6
PDF
Back to Basics 2017: Mí primera aplicación MongoDB
PDF
10gen MongoDB Video Presentation at WebGeek DevCup
PPTX
Whats new in MongoDB 24
Fast querying indexing for performance (4)
Nosql part 2
Mongo db eveningschemadesign
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Pros and Cons
MongoDB using Grails plugin by puneet behl
2012 phoenix mug
MongoDB and Ruby on Rails
Mongodb Introduction
Webinar: Was ist neu in MongoDB 2.4
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
Mongo db
Introduction to MongoDB
Intresting changes in mongo 2.6
Back to Basics 2017: Mí primera aplicación MongoDB
10gen MongoDB Video Presentation at WebGeek DevCup
Whats new in MongoDB 24

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Cloud computing and distributed systems.
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Approach and Philosophy of On baking technology
PDF
KodekX | Application Modernization Development
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The AUB Centre for AI in Media Proposal.docx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Cloud computing and distributed systems.
Network Security Unit 5.pdf for BCA BBA.
Approach and Philosophy of On baking technology
KodekX | Application Modernization Development
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Modernizing your data center with Dell and AMD
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
Big Data Technologies - Introduction.pptx

Mongo db tutorials

  • 1. An Introduction to MongoDB Anuj Jain Equal Experts India MongoDB
  • 4. Not a RDBMS • Mongo is not a relational database like MySQL, Oracle • No transactions. • No referential Integrity. • No Joins. • No schema, so no columns or rows. • NoSQL.
  • 5. What is MongoDB ? • Scalable High-Performance Open-source, Document-orientated database. • Built for Speed • Rich Document based queries for Easy readability. • Full Index Support for High Performance. • Replication and Failover for High Availability. • Auto Sharding for Easy Scalability. • Map / Reduce for Aggregation
  • 6. Quiz ? Which of the following statement are true about MongoDB ? 1. MongoDB is document oriented. 2. MongoDB supports Joins. 3. MongoDB has dynamic schema. 4. MongoDB supports SQL.
  • 7. What MongoDB is great for ? • Semi structured Content Management. • Real time Analytics and High-Speed Logging. • Caching and High Availability. • Mobile and Social Infrastructure, Big Data etc.
  • 8. Some considerations while designing schema in MongoDB • Combine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins). • Duplicate the data (but limited) because disk space is cheap as compare to compute time. • Do joins while write, not on read. • Optimize your schema for most frequent use cases. • Do complex aggregation in the schema.
  • 9. Example – Blog Post • Every post has the unique title, description and url. • Every post can have one or more tags. • Every post has the name of its publisher and total number of likes. • Every Post have comments given by users along with their name, message, data-time and likes. • On each post there can be zero or more comments.
  • 10. RDBMS
  • 11. Mongo Schema { “_id” : ObjectId("55b1f50899708bec87f96edc") “title” : “MongoDB Tutorial for beginner”, “description: “How to start using mongodb”, “by: Anuj Jain, “url: “http://mongodbtutorial.com/blog/mongodb”, “tags” : ['mongodb', 'nosql' ], “likes” : 200, “comments” : [ { “user” : ''MongoUser”, “message” : “Very Nice Tutorial” , “dateCreated” : NumberLong(1437725960469), “like” : true } ] }
  • 12. Quiz ? How many different data types are there in JSON ? 1. 4 2. 5 3. 6 4. 7
  • 13. Answer Ans: 6 1. String 2. Number 3. Boolean 4. null 5. Array 6. Object/document
  • 14. CRUD Create db.collection.insert( <document> ) db.collection.save( <document> ) db.collection.update( <query>, <update>, { upsert: true } ) Read db.collection.find( <query>, <projection> ) db.collection.findOne( <query>, <projection> ) Update db.collection.update( <query>, <update>, <options> ) Delete db.collection.remove( <query>, <justOne> )
  • 15. Some Other Operators 1. $and 2. $or 3. $in 4. $nin 5. $exists 6. $push 7. $pop 8. $addToSet
  • 16. Indexes 1.Indexes are special data structures, that store a small portion of the data set in an easy to traverse form. 2. Stores the value of a specific field or set of fields. 3. Ordered by the value of the field as specified in index. 4. Indexes can improves read operation but slower the write operations. 5. Mongodb use B-Tree data structure to store indexes. 6.Blocks mongod process unless you specify the background. 7. null is assumed if field is missing.
  • 17. When To Index ? 1. Frequently Queried Fields 2. Low response time 3. Sorting 4. Avoid full collection scans.
  • 18. Indexes Types 1. Default (_id) 2. Single Field 3. Compound Index 4. Multikey Index 5. Geospatial Index 6. Sparse Index 7. TTL Index
  • 19. Quiz ? Mongodb index can have keys of different types ( ints, dates, string for example) in it ? 1. True 2. False
  • 20. Covered Indexes Queries that can be resolved with only the index (does not need to fetch the original document) Example: { “name”:”Anuj”, – “age”:28, – “gender”:Male, – “skills”:[“Java”,”Mongo”] } db.people.ensureIndex({“name”:1,”age”:1}) db.people.find ({“name”:”Anuj”},{“_id” :0 , “age”:1})
  • 21. TTL Indexes {“time to live”} 1. Mongod already remove the data from the collections after specify number of seconds. 2. Field type should either be BSON date type or an array of BSON date- type object Eg. db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } ) Where createdAt is date field
  • 22. Quiz ? Suppose we run : db.foo.ensureIndex ({a:1, b:2, c:3}) db.foo.find({a : “sports”, b:{$gt : 100}}) Then 1.Only the index needs touched to fully execute the query. 2.Then index and some documents need to be executed.
  • 23. Why Replication? • To keep your data safe. • High (24*7) availability of data. • Disaster Recovery. • Read scaling (extra copies to read from). • No downtime for maintenance (like backups, index rebuilds, compaction).
  • 24. Replica set features • A class of N nodes. • Anyone node can be primary. • All write operation goes to primary. • Automatic fail-overs. • Automatic Recovery. • Consensus election of primary.
  • 25. Capped Collection • Fixed size circular queues that follow the insertion order. • Fixed size is preallocated and when it exhausted, oldest document will automatically start getting deleted. • We cannot delete documents from a capped collection. • There are no default indexes present in a capped collection, not even on _id field.
  • 26. Capped Collection Commands : 1. db.createCollection ( "cappedcollection", {capped:true,size:10000} ) 2. db.createCollection ( "cappedcollection", capped:true, size:10000, max:1000 }) 3. db.cappedLogCollection.isCapped() 4. db.runCommand({"convertToCapped":"posts",size:10000}) 5. db.cappedLogCollection.find().sort({$natural:-1})
  • 27. Query Limitations: Indexing can't be used in queries which use: 1. Regular expressions or negation operators like $nin, $not, etc. Arithmetic operators like $mod, etc. 2. $where clause Hence, it is always advisable to check the index usage for your queries.
  • 28. Index Limitation Maximum Ranges: • A collection cannot have more than 64 indexes. • The length of the index name cannot be longer than 125 characters. • A compound index can have maximum 31 fields indexed
  • 29. $explain The $explain operator provides information and statistics on the query for example : 1. Indexes used the query. 2. Number of document scan in serving the query. 3. Whether index enough to serve the query data i.e. covered Index. Usage : db.users.find({gender:"M"}, {user_name:1,_id:0} ).explain()
  • 30. $hint The $hint operator forces the query optimizer to use the specified index to run a query db.users.find({gender:"M"},{user_name:1,_id:0}) » .hint({gender:1,user_name:1})
  • 31. Backup & Restore Backup Utilities 1. mongodump (use to dump complete data directory or db) 2. mongoexport (use to dump certain collection to output file like json or csv). Restore Utilities 1. mongorestore 2. mongoimport
  • 32. ObjectId An ObjectId is a 12-byte BSON type having the following structure: The first 4 bytes representing the seconds since the unix epoch The next 3 bytes are the machine identifier The next 2 bytes consists of process id The last 3 bytes are a random counter value