Building your first app with MongoDB

Preview:

DESCRIPTION

Jump start presentation for developers to get started with MongoDB.

Citation preview

SA, MongoDB

Norberto Leite

#mongodbdays @mongodb @nleite #developers

Building your first app An introduction to MongoDB

First Things First!

Let’s not talk about Fußball!

Quick Introduction

•  Norberto Leite –  SA –  Madrid, Spain –  norberto@mongodb.com –  @nleite

Welcome to MongoDB Days Munich! This is YOUR conference!

Thanks for being part of the Family!

Grab our staff for anything you need!

What is MongoDB?

MongoDB is a ___________ database

•  Document

•  Open source

•  High performance

•  Horizontally scalable

•  Full featured

Document Database

•  Not for .PDF & .DOC files

•  A document is essentially an associative array

•  Document = JSON object

•  Document = PHP Array

•  Document = Python Dict

•  Document = Ruby Hash

•  etc

Open Source

•  MongoDB is an open source project

•  On GitHub

•  Licensed under the AGPL

•  Started & sponsored by MongoDB Inc (formerly 10gen)

•  Commercial licenses available

•  Contributions welcome

High Performance

•  Written in C++

•  Extensive use of memory-mapped files i.e. read-through write-through memory caching.

•  Runs nearly everywhere

•  Data serialized as BSON (fast parsing)

•  Full support for primary & secondary indexes

•  Document model = less work

Shard NShard 3Shard 2Shard 1

Horizontally Scalable

Database Landscape

Full Featured

•  Ad Hoc queries

•  Real time aggregation

•  Rich query capabilities

•  Strongly consistent

•  Geospatial features

•  Support for most programming languages

•  Flexible schema

Setting Expectations

•  What is MongoDB

•  How to develop with MongoDB

•  Scale with MongoDB

•  Analytics

•  MMS

•  Sharding

•  Setting the correct environment

Ready to become a Pro!

mongodb.org/downloads

$ tar –z xvf mongodb-osx-x86_64-2.6.5.tgz

$ cd mongodb-osx-i386-2.4.4/bin

$ mkdir –p /data/db

$ ./mongod

Running MongoDB

MacBook-Air-:~ $ mongo MongoDB shell version: 2.6.5 connecting to: test > db.test.insert({text: 'Welcome to MongoDB'}) > db.test.find().pretty() {

"_id" : ObjectId("51c34130fbd5d7261b4cdb55"), "text" : "Welcome to MongoDB"

}

Mongo Shell

Document Database

Terminology

RDBMS MongoDB

Table, View ➜ Collection

Row ➜ Document

Index ➜ Index

Join ➜ Embedded Document

Foreign Key ➜ Reference

Partition ➜ Shard

Let’s Build a Blog

Let’s Build a Blog

Let’s Build a Personal Data Hub!

First step in any application is

Determine your entities

Entities in our Data Hub

•  Accounts

•  Messages –  emails –  tweets –  comments –  streams

•  Notifications

In a relational base app We would start by doing schema design

Typical (relational) ERD

Messages

Email

Tweets

Facebook messages

Accounts

Alerts

Das ist beängstigende Sache

In a MongoDB based app We start building our app and let the schema evolve

MongoDB ERD Accounts

Alerts

- account - user - password - refresh_rate - uri

Messages

- text - user - time - retweets

- from - to - body - attachments

- id - time - account_id

- subscribers - channel - rate - period - metrics:[]

Working With MongoDB

Demo time

>db

test

> use datahub

switching to db datahub

Switch to Your DB

>var account = {

"name": "gmail",

"credentials": {

"user": "norberto@mongodb.com",

"password": "YOU WISH!"

},

"smtp": "smpt.gmail.com",

"tls": true

}

Create our first Document

>db

test

> use datahub

switching to db datahub

> db.accounts.insert( account )

Switch to Your DB

> db.accounts.insert(account)

Insert the Record

No collection creation necessary

> db.accounts.findOne()

{

"_id": ObjectId("54490561150027cc775b1019"),

"name": "gmail",

"credentials": {

"user": "norberto@mongodb.com",

"password": "YOU WISH!"

},

"smtp": "smpt.gmail.com",

"tls": true

}

Find One Record

_id

•  _id is the primary key in MongoDB

•  Automatically indexed

•  Automatically created as an ObjectId if not provided

•  Any unique immutable value could be used

ObjectId

•  ObjectId is a special 12 byte value

•  Guaranteed to be unique across your cluster

•  ObjectId("50804d0bd94ccab2da652599") |----ts-----||---mac---||-pid-||----inc-----| 4 3 2 3

> db.accounts.findOne()

{

"_id": ObjectId("54490561150027cc775b1019"),

"name": "gmail",

"credentials": {

"user": "norberto@mongodb.com",

"password": "YOU WISH!"

},

”last_access": ISODate("2014-10-30T13:09:36.724Z"),

"smtp": "smpt.gmail.com",

"tls": true

}

Rich Data Types

Strings

Date

Boolean

BSON

> db.messages.insert({

"_id" : ObjectId("54527e08257844421e64623f"),

"favorited" : false,

"contributors" : null,

"truncated" : false,

"text" : "converting to #java 8",

"in_reply_to_status_id" : null,

”hashtags”: [ "#java", ]

}

Inserting Messages (emails, tweets …)

> db.messages.insert({

"_id" : ObjectId("54523d2d25784427c6fabce1"),

"From" : "norberto@mongodb.com",

"To" : "mongodb-user@googlegroups.com",

"Date" : ISODate("2012-08-15T22:32:34Z"),

"body" : {

"text/plain" : ”Hello Munich, nice to see yalll!"

},

"Subject" : ”Live From MongoDB World"

})

Inserting Messages (emails, tweets …)

> db.message.find().pretty() { "_id" : ObjectId("54523d2d25784427c6fabce1"),

"From" : "norberto@mongodb.com", "To" : "mongodb-user@googlegroups.com", "Date" : ISODate("2012-08-15T22:32:34Z"),

"body" : { "text/plain" : ”Hello Munich, nice to see yalll!" },

"Subject" : ”Live From MongoDB World" } { "_id" : ObjectId("54527e08257844421e64623f"),

"favorited" : false, "contributors" : null, "truncated" : false,

"text" : "converting to #java 8", "in_reply_to_status_id" : null, ”hashtags”: [ "#java", ]

… }

Finding a Message

> db.article.find({"hashtags":"#java"}).pretty() {

"_id" : ObjectId("54527e08257844421e64623f"),

"favorited" : false,

"contributors" : null,

"truncated" : false,

"text" : "converting to #java 8, #programing ",

"in_reply_to_status_id" : null,

”hashtags”: [ "#java", "#programing"]

}

Querying An Array

query in JSON

> db.messages.update({

"_id" : ObjectId("54523d2d25784427c6fabce1") },

{$set: { opened:

{date: ISODate("2012-08-15T22:32:34Z"), user: ’Norberto'}

}

}) >

Using Update to Add a Comment

set new field on the document which is a subdocument

> db.message.findOne({"_id" : ObjectId("54523d2d25784427c6fabce1")}) {

"_id" : ObjectId("54523d2d25784427c6fabce1"),

"From" : "norberto@mongodb.com",

"To" : "mongodb-user@googlegroups.com",

"Date" : ISODate("2012-08-15T22:32:34Z"),

"body" : {

"text/plain" : ”Hello Munich, nice to see yalll!"

},

"Subject" : ”Live From MongoDB World”

"opened" : {"date": ISODate("2012-08-15T22:32:34Z"), "user": ’Norberto'}

}

Post with Comment Attached

Find document by primary key

MongoDB Drivers

Real applications are not built in the shell

MongoDB has native bindings for over 12 languages

Morphia

MEAN Stack

Java

Python

Perl

Ruby

Support for the most popular languages and frameworks

Great, I’m excited! What’s next?

docs.mongodb.org

Never Stop Learning!

Schema Design, Schema Design, Schema Design, Schema Design!

Legacy Migration

1.  Copy existing schema & some data to MongoDB

2.  Iterate schema design development Measure performance, find bottlenecks, and embed

1.  one to one associations first 2.  one to many associations next 3.  many to many associations

3.  Migrate full dataset to new schema

New Software Application? Embed by default

Embedding over Referencing •  Embedding is a bit like pre-joined data

–  BSON (Binary JSON) document ops are easy for the server

•  Embed (90/10 following rule of thumb) –  When the “one” or “many” objects are viewed in the context

of their parent –  For performance –  For atomicity

•  Reference –  When you need more scaling –  For easy consistency with “many to many” associations

without duplicated data

It’s All About Your Application

•  Programs+Databases = (Big) Data Applications

•  Your schema is the impedance matcher –  Design choices: normalize/denormalize, reference/embed –  Melds programming with MongoDB for best of both –  Flexible for development and change

•  Programs×MongoDB = Great Big Data Applications

We've introduced a lot of concepts here

IoT

MMS @

Scale Easily

Best Practices, Automated

Cut Management Overhead

Scalability @

DevOps @

Provision

Upgrade

Scale

Continuous Backup

Point-in-Time Recovery

Performance Alerts

Wrapping up …

•  MongoDB is a great Developers Tool

•  Designed for :

•  Scalability •  Flexibility •  Performance

•  Multipurpose

•  Great Ecosystem

Well Done !

Questions?

norberto@mongodb.com

Norberto Leite

#mongodbdays

Obrigado!