Transcript
Page 1: Building Your First MongoDB App

Job Title, MongoDB

Speaker Name

#ConferenceHashTag

Building your first app;an introduction to MongoDB

Page 2: Building Your First MongoDB App

What is MongoDB?

Page 3: Building Your First MongoDB App

MongoDB is a ___________ database

• Document

• Open source

• High performance

• Horizontally scalable

• Full featured

Page 4: Building Your First MongoDB App

Document Database

• Not for .PDF & .DOC files

• A document is essentially an associative array– JSON object

– PHP Array

– Python Dict

– Ruby Hash

– etc

Page 5: Building Your First MongoDB App

var user = {

username: ’erlichson',

first_name: ’Andrew',

last_name: ’Erlichson',

}

Start with an object (or array, hash, dict, etc)

Page 6: Building Your First MongoDB App
Page 7: Building Your First MongoDB App

Database Landscape

Page 8: Building Your First MongoDB App

Full Featured

• Ad Hoc queries

• Real time aggregation

• Rich query capabilities

• Strongly consistent

• Geospatial features

• Support for most programming languages

• Flexible schema

Page 9: Building Your First MongoDB App

mongodb.org/downloads

Page 10: Building Your First MongoDB App

$ tar –z xvf mongodb-osx-x86_64-2.4.x.tgz

$ cd mongodb-osx-i386-2.4.4/bin

$ mkdir –p /data/db

$ ./mongod

Running MongoDB

Page 11: Building Your First MongoDB App

MacBook-Air-:~ $ mongo

MongoDB shell version: 2.4.4

connecting to: test

> db.test.insert({text: 'Welcome to MongoDB'})

> db.test.find().pretty()

{

"_id" : ObjectId("51c34130fbd5d7261b4cdb55"),

"text" : "Welcome to MongoDB"

}

Mongo Shell

Page 12: Building Your First MongoDB App

Document Database

Page 13: Building Your First MongoDB App

Terminology

RDBMS MongoDB

Table, View ➜ Collection

Row ➜ Document

Index ➜ Index

Join ➜ Embedded Document

Foreign Key ➜ Reference

Partition ➜ Shard

Page 14: Building Your First MongoDB App

Let’s Build a Blog

Page 15: Building Your First MongoDB App

First step in any application is

Determine your entities

Page 16: Building Your First MongoDB App

Entities in our Blogging System

• Users (post authors)

• Article

• Comments

• Categories, Tags

Page 17: Building Your First MongoDB App

In a relational base app

We would start by doing schema design

Page 18: Building Your First MongoDB App

Typical (relational) ERD

Page 19: Building Your First MongoDB App

In a MongoDB based appWe start building our appand let the schema evolve

Page 20: Building Your First MongoDB App

MongoDB ERD

Page 21: Building Your First MongoDB App

Working With MongoDB

Page 22: Building Your First MongoDB App

var user = {

username: ’erlichson',

first_name: ’Andrew',

last_name: ’Erlichson',

}

Start with an object (or array, hash, dict, etc)

Page 23: Building Your First MongoDB App

>db

test

> use blog

switching to db blog

> db.users.insert( user )

Store in DB "blog", collection "users"

Page 24: Building Your First MongoDB App

>db

test

> use blog

switching to db blog

> db.users.insert( user )

Store in DB "blog", collection "users"

No collection creation necessary

Page 25: Building Your First MongoDB App

> db.users.find()

{

"_id" : ObjectId("50804d0bd94ccab2da652599"),

"username" : ”erlichson",

"first_name" : ”Andrew",

"last_name" : ”Erlichson"

}

> db.users.find( { "_id" : ObjectId(..) } )

> db.users.find( { "username" : "erlichson" } )

Find a Record

Page 26: Building Your First MongoDB App

> db.article.insert({

title: ‘Hello World’,

body: ‘This is my first blog post’,

date: new Date(‘2013-06-20’),

username: ‘erlichson’,

tags: [‘adventure’, ‘mongodb’],

comments: [ ]

})

Creating a Blog Post

Page 27: Building Your First MongoDB App

> db.article.find().pretty()

{

"_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"),

"title" : "Hello World",

"body" : "This is my first blog post",

"date" : ISODate("2013-06-20T00:00:00Z"),

"username" : "erlichson",

"tags" : [

"adventure",

"mongodb"

],

"comments" : [ ]

}

Finding the Post

Page 28: Building Your First MongoDB App

> db.article.find({tags:'adventure'}).pretty()

{

"_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"),

"title" : "Hello World",

"body" : "This is my first blog post",

"date" : ISODate("2013-06-20T00:00:00Z"),

"username" : "erlichson",

"tags" : [

"adventure",

"mongodb"

],

"comments" : [ ]

}

Querying An Array

Page 29: Building Your First MongoDB App

> db.article.update({_id:

new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

{$push:{comments:

{name: 'Steve Blank', comment: 'Awesome Post'}}})

>

Using Update to Add a Comment

Page 30: Building Your First MongoDB App

> db.article.findOne({_id: new ObjectId("51c3bcddfbd5d7261b4cdb5b")})

{

"_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"),

"body" : "This is my first blog post",

"comments" : [

{

"name" : "Steve Blank",

"comment" : "Awesome Post"

}

],

"date" : ISODate("2013-06-20T00:00:00Z"),

"tags" : [

"adventure",

"mongodb"

],

"title" : "Hello World",

"username" : "erlichson"

}

Post with Comment Attached

Page 31: Building Your First MongoDB App

MongoDB Drivers

Page 32: Building Your First MongoDB App
Page 33: Building Your First MongoDB App
Page 34: Building Your First MongoDB App

docs.mongodb.org

Page 35: Building Your First MongoDB App

Online Training at MongoDB University

Page 36: Building Your First MongoDB App

Questions?

Page 37: Building Your First MongoDB App

#ConferenceHashtag

Thank You