This presentation will describe MongoDB's document database and what advantages it has over traditional databases. The presentation will explore MongoDB's server, query language, ecosystem and various tools. Brett will demonstrate using various MongoDB tools to assist in developing a Python application that utilises MongoDB as the database.
MongoDB.local Sydney: An Introduction to Document Databases with MongoDB
1.
2. An Introduction to Document
Databases with MongoDB
Brett Gray, Senior Consulting Engineer, MongoDB
3. Why am I here?
About a decade ago, three guys with a lot of industry experience got together and thought
“There must be a better way”. A better way to store data, that was more intuitive and more
natural…
6. About Me
I’m a Senior Consulting Engineer in the Professional Services team at MongoDB.
In a previous life, I served on Submarines in the Royal Australian Navy and was a Principal Engineer at
Puppet…….I also have a Graduate Diploma of Brewing.
8. What is MongoDB?
MongoDB is a cross-platform and open-source document-oriented general purpose database. As a document
database, MongoDB doesn’t use table-based structure, rather it uses JSON-like documents that have dynamic
schemas which it calls BSON.
Please keep in mind that NoSQL does not mean no relationships!
9. What is MongoDB?
Server written in C++ and C
Tools written in C++, C, Go, Java, and JavaScript
Binary packages are available for download at mongodb.com/downloads
Source code for the server is available at github.com/mongodb
Ready for deployment in the cloud, on VMs or on bare metal
10. What is MongoDB?
MongoDB was written to make it simple to store data.
• {"Key": "Value"}
Documents map naturally to common data structures in most popular programming languages
Schemas are dynamic. There’s no need for expensive migrations.
As new data is encountered, just store it. If the shape of data needs to change, then change it.
We are schema-flexible, but it is important to be consistent
11. What is MongoDB?
MongoDB is an ecosystem of products, including a database, visual data explorer, platform, and analytics tools to
make it easier than ever to develop your applications.
14. Not just key-
valueMongoDB’s document model is not just a key-value
store.
Rich document composition is possible, and
encouraged.
Store embedded objects, arrays, rich data types, and
more.
{
"title": "The Martian",
"year": 2015,
"runtime": 130,
"released": ISODate("2015-10-
02T00:00:00Z"),
"cast": [
"Kate Mara",
"Matt Damon",
"Jessica Chastain",
"Kristen Wiig"
]
}
18. Document Model
It’s a natural way to think about data, and maps incredibly well to patterns we know and common types and
structures we use daily.
The schema should be modelled from how you access your data.
As highlighted, the schema is flexible, but you can use schema validation within in your application and within the
database to enforce your schema.
20. Query Language
CREATE TABLE movies (
id MEDIUMINT NOT NULL
AUTO_INCREMENT,
title Varchar(30),
year Number,
rating Number,
PRIMARY KEY (id)
)
db.createCollection("movies")
21. Query Language
INSERT INTO movies(title,
year, rating) VALUES ("The
Shawshank Redemption", 10,
1994)
db.movies.insertOne({title:
"The Shawshank Redemption",
year: 1994, rating: 10})
22. Query Language
ALTER TABLE movies ADD cast
ARRAY
UPDATE movies SET cast =
["Morgan Freeman", "Tim
Robbins"] WHERE title = "The
Shawshank Redemption"
db.movies.updateOne(
{title: "The Shawshank
Redemption"},
{$set: { "cast": ["Morgan
Freeman", "Tim Robbins" ] } }
)
23. Query Language
ALTER TABLE movies ADD cast
ARRAY
UPDATE movies SET cast =
["Morgan Freeman", "Tim
Robbins"] WHERE title = "The
Shawshank Redemption"
db.movies.updateOne(
{title: "The Shawshank
Redemption"},
{$set: { "cast": ["Morgan
Freeman", "Tim Robbins" ] } }
)
24. Query Language
SELECT * FROM movies
SELECT * FROM movies WHERE
rating > 7
SELECT * FROM movies WHERE
rating > 7 LIMIT 1
SELECT * FROM movies WHERE
rating > 7 AND year > 1990
??
db.movies.find()
db.movies.find({rating: { $gt:
7 }})
db.movies.findOne({rating: {
$gt: 7 }}).limit(1)
db.movies.find({rating: { $gt:
7 }, year: { $gt: 1990 } })
db.movies.find({cast: "Morgan
Freeman"})
26. Query Language - Performance
Indexes support the efficient execution of queries in MongoDB.
Many index types
• Single Field
• Multikey
• Text
• Geospatial
• Hashed
• Compound
27. Query Language - Aggregation
A framework for data aggregation modelled on the concept of data processing pipelines.
Documents enter a multi-stage pipeline that transforms the documents into aggregated results.
Reduce the CPU cycles in processing your app, let MongoDB handle aggregation.
Results produced from aggregation lower in total byte size of your data, saving bandwidth.
28. Query Language - Aggregation
A framework for data aggregation modeled on the concept of data processing pipelines.
Documents enter a multi-stage pipeline that transforms the documents into aggregated results.
32. Resiliency – The Replica Set
Your application talks to MongoDB through the
driver, which is constantly monitoring your replica
set health.
Internally, the replica set is monitoring health as
well, ready to react in case of emergency.
34. Resiliency – The Replica Set
Your application talks to MongoDB through the
driver, which is constantly monitoring your replica
set health.
Internally, the replica set is monitoring health as
well, ready to react in case of emergency.
43. Atlas – So Much More
Monitoring and Alerting
• Supports Third-Party integration
• Webhooks
• Email
• SMS
• PagerDuty
• Slack
Live Migration
Backups and Recovery
BI Connector
We have community and enterprise editions
Works on Linux 64bit, Windows 64bit, and macOS
Open-source, you can find the source code on Github
Document-oriented….we go into this further. Uses BSON, which is binary-enclosed presentation of JSON that provides further data types, ordering etc
There are still relationships!
Think about hashtables, dictionaries etc
Mongodb
Compass
Charts
BI Connector
Basic Relational schema…….how many tables do I need to join to see a what makes and models of cars a person owns?
Point out that a multiple people can belong to multiple professions. And multiple people can own multiple cars (joint ownership)
This gets compared to a document schema in the next slide
Notice the order of our insert is different….because we do not care!
Some RDMS do have array support
The next slide just shows that some RDBMS will not handle ARRAYs (or objects for that matter)
Using Aussie spelling here….
The conveyor belt image is next
Output of one stage is the input for the next stage
Use Compass to see examples of how to use Aggregation
Highlight that people have different meanings for High Availability and Resiliency……..and it is not sharding
Resiliency, not scaling!