29
A Guide to the MEAN Stack Tips and Tricks for using MongoDB and NodeJS in overwhelmingly awesome ways Valeri Karpov CTO, Ascot Project www.thecodebarbarian.com @code_barbarian github.com/vkarpov15

The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

  • Upload
    mongodb

  • View
    41.281

  • Download
    3

Embed Size (px)

DESCRIPTION

Tips and tricks for using MongoDB and Node.js

Citation preview

Page 1: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

A Guide to the MEAN Stack

Tips and Tricks for using MongoDB and NodeJS in overwhelmingly awesome ways

Valeri KarpovCTO, Ascot Project

www.thecodebarbarian.com@code_barbarian

github.com/vkarpov15

Page 2: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

What is The Ascot Project?

Making fashion geek-friendly

Less like this More like this

Page 3: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Who is this guy?

- Academy for Telecommunications and Computer Science '07, Princeton CS '11- Co-founder and CTO, SCVNGR in '08- Google intern '09, mentored by Misko Hevery- Tower Research Capital '11 – '13- Developed Ascot Project at AngelHack DC in '12

Page 4: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

What is this all about?

- Why Ascot uses MongoDB- What's the MEAN stack and why its awesome- How MongoDB fits in the MEAN stack- Why MongooseJS is important and tradeoffs in it's usage- Example of a cool MEAN stack web app

Page 5: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Why MongoDB?

1) It's cool2) No joins for nested data3) Flexible and simple

Page 6: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

SQL is Annoying

Page 7: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

MEAN Stack

Web dev framework for NodeJS

Superheroic frontend framework

Event-based concurrency environment

Page 8: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

MEAN Stack – Why do I care?

- Write one language- With MongoDB and Mongoose, easy and flexible data validation- With NodeJS, never need threads- With AngularJS, dynamic client-side templates

Page 9: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Same Language, Same Objects

{ "_id" : ObjectId("5161a58b46341f8a46000003"), "username" : "vkarpov" }

{ "_id" : "5161a58b46341f8a46000003", "username" : "vkarpov" }

{ "_id" : "5161a58b46341f8a46000003", "username" : "vkarpov" }

Page 10: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

MongooseJS – code-defined schemas for MongoDB + NodeJS

- MongoDB and NodeJS are best friends

- No built-in schemas or validation

- But, MongoDB native wrapper for NodeJS is lacking:

Page 11: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

MongooseJS – code-defined schemas for MongoDB + NodeJS

Primary MongooseJS types:- Schema – structure defining how documents look- Connection – Wrapper around database connection- Model = Schema + Connection + collection name- Document is an instantiation of a Model

Page 12: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

A Basic MongooseJS Schema

Page 13: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

Using the StockPrice Model

Page 14: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

Sometimes being able to save anything isn't right

Page 15: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

Helpful Error Messages

- Nested JSON errors summary- Can use this for very generic form validation- More on this later

Page 16: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

Nesting and populating MongooseJS Schemas – an important distinction that doesn't exist in SQL

Populate – similar to SQL joinNest – re-use schema in another schema

Page 17: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

Can't do multi-level population!

Page 18: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

How MongooseJS Works

Nesting and populating MongooseJS Schemas

When can we use nested schemas?

Many-to-oneOne-to-manyOne-to-one

Many-to-many

Page 19: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

MEAN Stack Superpowers

NodeJS is not just a Javascript web server

- Event based concurrency- Package manager similar to Rails gems- Makes multithreaded servers easy

Page 20: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Example: Bitcoins!

Page 21: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Bitcoin P&L

Step 1 : Web app to display a streaming Bitcoin ticker- Complex problem – multiple threads, sockets, mutex, etc.- People in HFT get paid crazy money to do this in C++

Page 22: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Bitcoin P&L

- Enter NodeJS, where this is a back-of-napkin type problem

Page 23: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Bitcoin P&L

- Update frontend periodically? AngularJS makes it easy

Page 24: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Bitcoin P&L

Takeaways

- General problem : your server connecting to other servers- Example : Ascot links- Can write multi-threaded servers, but why?

- General problem : updating page without reload- AngularJS two-way data binding

Page 25: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Bitcoin P&L + MongoDB

Model – list of nested stock schemas

Routes – get list of stocks, add a new stock

Page 26: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Bitcoin P&L + MongoDB

AngularJS – generic no-reload form wrapper

Page 27: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Bitcoin P&L + MongoDB

Jade – A View To A Kill And the result:

Page 28: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Takeaways

- MEAN Stack is awesome- Sophisticated frontends- Multithreaded backends made easy- MongooseJS helps NodeJS and MongoDB play nice- Limitations on MongooseJS population

Page 29: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js

Thanks for Listening!

Further Reading

- Ascot Project : www.ascotproject.com- AngularJS Form Validation : “How to Easily Validate Any Form Ever Using AngularJS” @ www.thecodebarbarian.com- SocketIO Bitcoin Price Streaming : mtgox-socket-client on NPM and Github- Bitcoin realtime P&L : github.com/vkarpov15/bitcoin-pnl, coming soon to www.thecodebarbarian.com