Build innovative modern applications that
create a competitive advantage.
Hossein Boustani
Developer
@engboustani
Introducing
• MongoDB is an open-source, document database designed for ease
of development and scaling.
Database Types
RDBMS
(Relational Database
Management System)
SQL Server, Mysql, Oracle
NoSQL
(Non-relational
Operational Stores)
MangoDB, Cassandra, Redis
Document-oriented database
• Document-oriented databases are inherently a subclass of the
key-value store, another NoSQL database concept.
• Document databases contrast strongly with the traditional relational
database (RDB):
• Relational databases generally store data in separate tables that are defined
by the programmer, and a single object may be spread across several tables.
• Document databases store all information for a given object in a single
instance in the database, and every stored object can be different from every
other.
Database Landscape
Memcached
MongoDB
RDBMS
Depth of Functionality
Scalability&Performance
Terminology
RDBMS MongoDB
Table, View Collection
Row Document
Index Index
Join Embedded Document
Foreign Key Reference
Partition Shard
Features
• Documents are stored in BSON (binary JSON)
• Rich, javascript-based query syntax
• Allows us to deep, nested queries
• Efficient binary large object store via GridFS
• i.e. store images, videos, anything
Let’s Build a Blog with MongoDB
• Entities in our Blogging System:
• Users
• Articles
• Comments
• Tags
• Categories
Typical (relational) ERD
Category
• Name
• URL
Tag
• Name
• URL
Comment
• Comment
• Date
• Author
User
• Name
• Email Address
Article
• Name
• Slug
• Publish Date
• Text
MongoDB ERD
Article
• Name
• Slug
• Publish Date
• Text
• Author
Comment[]
• Comment
• Date
• Author
Tag[]
• Value
Category[]
• Value
User
• Name
• Email Address
BSON
• BSON is a computer data interchange format used mainly as a data
storage and network transfer format in the MongoDB database. It is a
binary form for representing simple data structures, associative
arrays (called objects or documents in MongoDB), and various data
types of specific interest to MongoDB. The name "BSON" is based on
the term JSON and stands for "Binary JSON".
Start with an object (or array, hash, dict, etc)
> var user = {
username: ‘eng.boustani’,
first_name: ‘Hossein’,
last_name: ‘Boustani’
}
Insert the Record
> db
test
> use blog
switched to db blog
// syntax is db.<collection>.<command>
> db.users.insert(user)
Retrieve the Record again
// get one document (don’t care which one)
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"username" : "eng.boustani",
"first_name" : "Hossein",
"last_name" : "Boustani"
}
_id
• _id is the primary key in MongoDB
• Any unique immutable value could be used
• Automatically created as an ObjectId if not provided
• Automatically indexed
ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599")
ts mac pid inc
Creating a Blog Article
> db.articles.insert({
title: ‘Hello World’,
body: ‘This is my first blog post’,
date: new Date(‘2013-06-20’),
username: ‘eng.boustani’,
tags: [‘adventure’, ‘mongodb’],
comments: [ ]
})
Finding the Article
> db.articles.find().pretty()
{
"_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"),
"title" : "Hello World",
"body" : "This is my first blog post",
"date" : ISODate("2013-06-20T00:00:00Z"),
"username" : "eng.boustani",
"tags" : [ "adventure", "mongodb"],
"comments" : [ ]
}
Finding the Article
> db.articles.find({
_id : ObjectId("51c3bafafbd5d7261b4cdb5a")
}).pretty()
{
"_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"),
"title" : "Hello World",
"body" : "This is my first blog post",
"date" : ISODate("2013-06-20T00:00:00Z"),
"username" : "eng.boustani",
"tags" : [ "adventure", "mongodb"],
"comments" : [ ]
}
Querying An Array
> db.articles.find({ tags : ‘adventure’ }).pretty()
{
"_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"),
"title" : "Hello World",
"body" : "This is my first blog post",
"date" : ISODate("2013-06-20T00:00:00Z"),
"username" : "eng.boustani",
"tags" : [ "adventure", "mongodb"],
"comments" : [ ]
}
Using Update to Add a Comment
// the syntax is: update( what, how)
> db.articles.update({
_id: ObjectId("51c3bcddfbd5d7261b4cdb5b")
}, {
"$push" : {
"comments" : {
"name" : "Asghar Sohrabi",
"comment" : "Awesome Post"
}
})
Article with Comment Embedded
> db.articles.find({ username: "eng.boustani" }).pretty()
{
"_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"),
"body" : "This is my first blog post",
"comments" : [
{
"name" : "Asghar Sohrabi",
"comment" : "Awesome Post"
}
],
"date" : ISODate("2013-06-20T00:00:00Z"),
"tags" : ["adventure", "mongodb"],
"title" : "Hello World",
"username" : "eng.boustani"
}
Remove Comments / Articles
// remove comment with $pull
> var last_comment = {
"name" : "Asghar Sohrabi",
"comment" : "Awesome Post"
}
> db.articles.update({
_id: ObjectId("51c3bcddfbd5d7261b4cdb5b")
}, {
$pull : { comments: last_comment }
})
// remove article
> db.articles.remove({
_id: ObjectId("51c3bcddfbd5d7261b4cdb5b")
})
MongoDB Drivers
• MongoDB has native bindings for over 12 languages
MongoDB Tools
• Robomongo
• MongoDB Compass
• MongoDB Cloud Manager
• MongoMonitor
Some Companies using MongoDB
Book
MongoDB: The Definitive Guide
Kristina Chodorow
Book
MongoDB and Python
Niall O'Higgins
Book
PHP and MongoDB Web
Development
Rubayeet Islam
Resources
• mongodb.org
• tutorialspoint.com/mongodb/
• github.com/mongodb/
Question?
Thank You
Your time is limited, so don’t waste it
Living someone else’s life
-Steve Jobs

Introduction to MongoDB

  • 1.
    Build innovative modernapplications that create a competitive advantage.
  • 2.
  • 3.
    Introducing • MongoDB isan open-source, document database designed for ease of development and scaling.
  • 4.
    Database Types RDBMS (Relational Database ManagementSystem) SQL Server, Mysql, Oracle NoSQL (Non-relational Operational Stores) MangoDB, Cassandra, Redis
  • 5.
    Document-oriented database • Document-orienteddatabases are inherently a subclass of the key-value store, another NoSQL database concept. • Document databases contrast strongly with the traditional relational database (RDB): • Relational databases generally store data in separate tables that are defined by the programmer, and a single object may be spread across several tables. • Document databases store all information for a given object in a single instance in the database, and every stored object can be different from every other.
  • 6.
    Database Landscape Memcached MongoDB RDBMS Depth ofFunctionality Scalability&Performance
  • 7.
    Terminology RDBMS MongoDB Table, ViewCollection Row Document Index Index Join Embedded Document Foreign Key Reference Partition Shard
  • 8.
    Features • Documents arestored in BSON (binary JSON) • Rich, javascript-based query syntax • Allows us to deep, nested queries • Efficient binary large object store via GridFS • i.e. store images, videos, anything
  • 9.
    Let’s Build aBlog with MongoDB • Entities in our Blogging System: • Users • Articles • Comments • Tags • Categories
  • 10.
    Typical (relational) ERD Category •Name • URL Tag • Name • URL Comment • Comment • Date • Author User • Name • Email Address Article • Name • Slug • Publish Date • Text
  • 11.
    MongoDB ERD Article • Name •Slug • Publish Date • Text • Author Comment[] • Comment • Date • Author Tag[] • Value Category[] • Value User • Name • Email Address
  • 12.
    BSON • BSON isa computer data interchange format used mainly as a data storage and network transfer format in the MongoDB database. It is a binary form for representing simple data structures, associative arrays (called objects or documents in MongoDB), and various data types of specific interest to MongoDB. The name "BSON" is based on the term JSON and stands for "Binary JSON".
  • 13.
    Start with anobject (or array, hash, dict, etc) > var user = { username: ‘eng.boustani’, first_name: ‘Hossein’, last_name: ‘Boustani’ }
  • 14.
    Insert the Record >db test > use blog switched to db blog // syntax is db.<collection>.<command> > db.users.insert(user)
  • 15.
    Retrieve the Recordagain // get one document (don’t care which one) > db.users.findOne() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : "eng.boustani", "first_name" : "Hossein", "last_name" : "Boustani" }
  • 16.
    _id • _id isthe primary key in MongoDB • Any unique immutable value could be used • Automatically created as an ObjectId if not provided • Automatically indexed
  • 17.
    ObjectId • ObjectId isa special 12 byte value • Guaranteed to be unique across your cluster • ObjectId("50804d0bd94ccab2da652599") ts mac pid inc
  • 18.
    Creating a BlogArticle > db.articles.insert({ title: ‘Hello World’, body: ‘This is my first blog post’, date: new Date(‘2013-06-20’), username: ‘eng.boustani’, tags: [‘adventure’, ‘mongodb’], comments: [ ] })
  • 19.
    Finding the Article >db.articles.find().pretty() { "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : "eng.boustani", "tags" : [ "adventure", "mongodb"], "comments" : [ ] }
  • 20.
    Finding the Article >db.articles.find({ _id : ObjectId("51c3bafafbd5d7261b4cdb5a") }).pretty() { "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : "eng.boustani", "tags" : [ "adventure", "mongodb"], "comments" : [ ] }
  • 21.
    Querying An Array >db.articles.find({ tags : ‘adventure’ }).pretty() { "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : "eng.boustani", "tags" : [ "adventure", "mongodb"], "comments" : [ ] }
  • 22.
    Using Update toAdd a Comment // the syntax is: update( what, how) > db.articles.update({ _id: ObjectId("51c3bcddfbd5d7261b4cdb5b") }, { "$push" : { "comments" : { "name" : "Asghar Sohrabi", "comment" : "Awesome Post" } })
  • 23.
    Article with CommentEmbedded > db.articles.find({ username: "eng.boustani" }).pretty() { "_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"), "body" : "This is my first blog post", "comments" : [ { "name" : "Asghar Sohrabi", "comment" : "Awesome Post" } ], "date" : ISODate("2013-06-20T00:00:00Z"), "tags" : ["adventure", "mongodb"], "title" : "Hello World", "username" : "eng.boustani" }
  • 24.
    Remove Comments /Articles // remove comment with $pull > var last_comment = { "name" : "Asghar Sohrabi", "comment" : "Awesome Post" } > db.articles.update({ _id: ObjectId("51c3bcddfbd5d7261b4cdb5b") }, { $pull : { comments: last_comment } }) // remove article > db.articles.remove({ _id: ObjectId("51c3bcddfbd5d7261b4cdb5b") })
  • 25.
    MongoDB Drivers • MongoDBhas native bindings for over 12 languages
  • 26.
    MongoDB Tools • Robomongo •MongoDB Compass • MongoDB Cloud Manager • MongoMonitor
  • 27.
  • 28.
    Book MongoDB: The DefinitiveGuide Kristina Chodorow
  • 29.
  • 30.
    Book PHP and MongoDBWeb Development Rubayeet Islam
  • 31.
  • 32.
  • 33.
    Thank You Your timeis limited, so don’t waste it Living someone else’s life -Steve Jobs