46
Evening with MongoDB Building your first MEAN app Solution Architect, MongoDB, Inc. [email protected] Dmitry Baev

Building your First MEAN App

  • Upload
    mongodb

  • View
    2.202

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Building your First MEAN App

Evening with MongoDBBuilding your first MEAN app

Solution Architect, MongoDB, Inc.

[email protected]

Dmitry Baev

Page 2: Building your First MEAN App

Agenda

• Designing your schema in MongoDB

• How to be MEAN

• Visualizing your MongoDB data in a MEANingful way!

• Scalability & Availability

• Deployment Architectures & Operations

Page 3: Building your First MEAN App

The Document Model

Page 4: Building your First MEAN App

Terminology

MongoDB RDBMS

Collection Table

Document Row

Index Index

Embedded Document Join

Reference Foreign Key

Page 5: Building your First MEAN App

Relational:Focus on data storage

Document:Focus on data use

Page 6: Building your First MEAN App

Relational:What answers do I have?

Document:What questions do I have?

Page 7: Building your First MEAN App

7

Document Data Model

Relational MongoDB

{ first_name: ‘Paul’, surname: ‘Miller’, city: ‘London’, location: [45.123,47.232], cars: [ { model: ‘Bentley’, year: 1973, value: 100000, … }, { model: ‘Rolls Royce’, year: 1965, value: 330000, … } }}

Page 8: Building your First MEAN App

8

Documents are Rich Data Structures

{ first_name: ‘Paul’, surname: ‘Miller’, cell: ‘+447557505611’ city: ‘London’, location: [45.123,47.232], Profession: [banking, finance, trader], cars: [ { model: ‘Bentley’, year: 1973, value: 100000, … }, { model: ‘Rolls Royce’, year: 1965, value: 330000, … } }}

Fields can contain an array of sub-documents

Fields

Typed field values

Fields can contain arrays

String

Number

Geo-

Coordinate

s

Page 9: Building your First MEAN App

9

Document Model Benefits

• Agility and flexibility– Data model supports business change– Rapidly iterate to meet new requirements

• Intuitive, natural data representation– Eliminates ORM layer– Developers are more productive

• Reduces the need for joins, disk seeks– Programming is more simple– Performance delivered at scale

Page 10: Building your First MEAN App

10

Developers are more productive

Page 11: Building your First MEAN App

11

Developers are more productive

Page 12: Building your First MEAN App

12

Better Data Locality

Performance

In-Memory Caching

In-Place Updates

Page 13: Building your First MEAN App

14

Do More With Your Data

MongoDBRich Queries

• Find Paul’s cars• Find everybody in London with a car

built between 1970 and 1980

Geospatial• Find all of the car owners within 5km of

Trafalgar Sq.

Text Search• Find all the cars described as having

leather seats

Aggregation• Calculate the average value of Paul’s

car collection

Map Reduce• What is the ownership pattern of colors

by geography over time? (is purple trending up in China?)

{ first_name: ‘Paul’, surname: ‘Miller’, city: ‘London’, location: [45.123,47.232], cars: [ { model: ‘Bentley’, year: 1973, value: 100000, … }, { model: ‘Rolls Royce’, year: 1965, value: 330000, … } }}

Page 14: Building your First MEAN App

15

Drivers & Ecosystem

Drivers

Support for the most popular languages and frameworks

Frameworks

MorphiaMEAN Stack

Java

Python

Perl

Ruby

Page 15: Building your First MEAN App

Modeling Data with MongoDB

Page 16: Building your First MEAN App

Contact

Address

Business Card

Page 17: Building your First MEAN App

Referencing

Addresses

{_id : 1,street : “10260 Bandley Dr”,city : “Cupertino”,state : “CA”,zip_code : ”95014”,country : “USA”

}

Contacts

{ _id : 2, name : “Steven Jobs”, title : “VP, New Product Development”, company : “Apple Computer”, phone : “408-996-1010”, address_id : 1}

Page 18: Building your First MEAN App

Embedding

Contacts

{ _id : 2, name : “Steven Jobs”, title : “VP, New Product Development”, company : “Apple Computer”, address : {

street : “10260 Bandley Dr”,city : “Cupertino”,state : “CA”,zip_code : ”95014”,country : “USA”

}, phone : “408-996-1010”}

Page 19: Building your First MEAN App

Schema Flexibility

{ name : “Steven Jobs”, title : “VP, New Product Development”, company : “Apple Computer”, address : {

street : “10260 Bandley Dr”,city : “Cupertino”,state : “CA”,zip_code : ”95014”

}, phone : “408-996-1010”}

{ name : “Larry Page”, url : “http://google.com/”, title : “CEO”, company : “Google!”, email : “[email protected]”, address : { street : “555 Bryant, #106”, city : “Palo Alto”, state : “CA”, zip_code : “94301” } phone : “650-618-1499”, fax : “650-330-0100”}

Page 20: Building your First MEAN App

{ “_id”: 2, “name”: “Steven Jobs”, “title”: “VP, New Product Development”, “company”: “Apple Computer”, “address”: [

{ “street”: “10260 Bandley Dr”, “city”: “Cupertino”, “state”: “CA”, “zip_code”: ”95014”, “country”: “USA”

},{ “street”: “52 Spring St”, “city”: “Block Island”, “state”: “RI”

} ], “phone”: “408-996-1010”}

Schema Flexibility

{ _id : 2, name : “Steven Jobs”, title : “VP, New Product Development”, company : “Apple Computer”, address : {

street : “10260 Bandley Dr”,city : “Cupertino”,state : “CA”,zip_code : ”95014”,country : “USA”

}, phone : “408-996-1010”}

Page 21: Building your First MEAN App

Let’s get MEAN

Page 22: Building your First MEAN App

MEAN Stack

Database

Server

ClientAngularjs-nvd3

-----------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------

Page 23: Building your First MEAN App

How to be MEAN!ServerClient

------------------------------------------------------

View

Controller

Service Route

Controller

Model

REST API call

Page 24: Building your First MEAN App

Node.js MongoDB Driver

• Install using npm

• Basic CRUD

• Mongos

• Aggregation

• Replica Set

• Sharding

• Read preferences

Page 25: Building your First MEAN App

Mongoose

• Provides “elegant MongoDB object modeling for

Node.js” using Schemas and Models

• Each schema maps to a MongoDB collection

• Models can be instantiated based on a schema

• Instances of models represent documents in

MongoDB

• Support for CRUD, aggregation, validation, pseudo-

joins, rich query builder + lots more!

• Install using npm

Page 26: Building your First MEAN App

Mongoose (getting started)• Require Node module

• Connect to MongoDB

• Define your schema in JSON

• Create the model object

Page 27: Building your First MEAN App

Mongoose (example)• Create/ Update

– #model.save()

• Read– #model.findOne()– #model.find()

• Update– #model.update()

• Delete– #model.remove()

• Aggregate– #model.aggregate()

Page 28: Building your First MEAN App

Visualizing data

• D3.js – JavaScript library for manipulating documents based on data – bring your data to life

• NVD3.js – reusable charts for d3.js

• Angularjs-nvd3-directives – Angular’s way of extending HTML to create new elements and functionality

Page 29: Building your First MEAN App

D3.js

NVD3.js

Visualizing data

Angularjs-nvd3-directive

s

Page 30: Building your First MEAN App

Real-time streaming charts

• Socket.IO (Node.js module)– “aims to make realtime apps possible in every browser and mobile

device”

• MongoDB Tailable Cursors– requires a capped collection

• NVD3.js Event Listeners– charts can be refreshed continuously as data is inserted/

update in MongoDB collection

Page 31: Building your First MEAN App

Demo:Visualizing your MongoDB data in a MEANingful way!

Page 33: Building your First MEAN App

Getting started with MEAN

• MEAN.io – ready to use MEAN stack boilerplate

• Yeoman – scaffolding tool for building modern web

apps– Community MEAN stack generators:

• https://github.com/wlepinski/generator-meanstack• https://github.com/jrcryer/generator-mean• https://github.com/chrisenytc/generator-meanis

Page 34: Building your First MEAN App

Scalability & Availability

Page 35: Building your First MEAN App

36

Scalability

Auto-Sharding

• Increase capacity as you go

• Commodity and cloud architectures

• Improved operational simplicity and cost visibility

Page 36: Building your First MEAN App

37

High Availability

• Automated replication and failover

• Multi-data center support

• Improved operational simplicity (e.g., HW swaps)

• Data durability and consistency

Page 37: Building your First MEAN App

38

MongoDB Architecture

Page 38: Building your First MEAN App

Deployment Architectures & Operations

Page 39: Building your First MEAN App

40

Single Data Center

• Automated failover

• Tolerates server failures

• Tolerates rack failures

• Number of replicas defines failure tolerance

Primary – A Primary – B Primary – C

Secondary – A Secondary – ASecondary – B

Secondary – BSecondary – CSecondary – C

Page 40: Building your First MEAN App

41

Active/Active Data Center

• Tolerates server, rack, data center failures, network partitions

Data Center - West

Primary – A Primary – B Primary – C

Secondary – A Secondary – BSecondary – C

Data Center - East

Secondary – A Secondary – B Secondary – C

Secondary – B Secondary – C Secondary – A

Data Center - Central

Arbiter – A Arbiter – B Arbiter – C

Page 41: Building your First MEAN App

42

Read Global/Write Local

Primary:NYC

Secondary:NYC

Primary:LON

Primary:SYD

Secondary:LON

Secondary:NYC

Secondary:SYD

Secondary:LON

Secondary:SYD

Page 42: Building your First MEAN App

References

• MEAN.io - http://www.mean.io

• MongoDB - http://mongodb.com

• Express - http://expressjs.com

• Angular - http://angularjs.org

• Node - http://nodejs.org

• Socket.IO - http://http://socket.io

• Angularjs-nvd3-directives - http

://cmaurer.github.io/angularjs-nvd3-directives

Page 43: Building your First MEAN App

mongodb.org/downloads

Page 44: Building your First MEAN App

Online Training at MongoDB University

Page 45: Building your First MEAN App

For More Information

Resource Location

MongoDB Downloads mongodb.org/downloads

Free Online Training education.mongodb.com

Webinars and Events mongodb.com/events

White Papers mongodb.com/white-papers

Case Studies mongodb.com/customers

Presentations mongodb.com/presentations

Documentation docs.mongodb.org

Additional Info [email protected]

Resource Location

Page 46: Building your First MEAN App

Thank You

Solution Architect, MongoDB, Inc.

[email protected]

Dmitry Baev