SlideShare a Scribd company logo
Valeri Karpov
Node.js Engineer, MongoDB
www.thecodebarbarian.com
github.com/vkarpov15
@code_barbarian
*
•MongoDB is great for storing web/mobile app data
•So let’s build a REST API using Node.js!
•+ learn a bit about test-driven dev with Node.js
•+ learn two MongoDB schema design principles
•Server-side only - just JSON, no HTML
•No AngularJS part, focus more on MongoDB
•Upcoming EdX course
*
•Part 1: Shopping Cart Application
• Search for products
• Add them to your cart
• Check out with Stripe
•Part 2: Using the Mongoose ODM
•Part 3: Schema Design
•Part 4: Building an API with the Express framework
•Part 5: Testing with Mocha + Superagent
*
*
*
*
•"Bad programmers worry about the code. Good
programmers worry about data structures and their
relationships." - Linus Torvalds
•3 schemas for 3 collections:
•Products
•Categories
•Users
*
•Product belongs to one or more categories
•Users can have multiple products in their cart
•Representing relationships in MongoDB is tricky
•But that’s what mongoose is for
*
•“Object document mapper” (like ORM, but for
MongoDB)
•“MongoDB object modeling designed to work in an
asynchronous environment”
•Written for Node.js
•Provides schema validation, pseudo-JOINs, etc.
*
•require()
● Async I/O
*
Register event
handler
In node.js, you don’t execute I/O imperatively.
You register a callback to execute when the I/O is done
Prints before
“done reading”
*
matches X@Y.Z
*
Create User
Save user to MongoDB
Load user from MongoDB
Print user to stdout
*
•Mongoose provides several neat features
• Model part of MVC
• Default values
• Schema validation and declarative schema design
*
•3 schemas:
• Product
• Category
• User
•Going to use mongoose to define schemas
•Will use a couple key schema design principles
*
*
*
*
•What categories are descendants of “Electronics”?
•
•
•What categories are children of “Non-Fiction”?
•
•What categories are ancestors of “Phones”?
*
*
•Queries in MongoDB should be simple
•Strive for minimal data transformation by server
•“Store what you query for”
•“If you need [the aggregation framework in a heavily
used API endpoint], you're screwed anyway, and should
fix your program.” - Linus Torvalds
•Good for performance and developer sanity
*
User’s cart as an array
of ObjectIds...
*
•Product and user = many-to-many relationship
•Don’t necessarily need a mapping table
•User won’t have 1000s of products in cart
•Can represent relationship as array in user since one
side is small
•If one side of many-to-many is bounded and/or
small, it is a good candidate for embedding
•Arrays that grow without bound are an antipattern!
• 16mb document size limit
• network overhead
*
•Most popular Node.js web framework
•Simple, pluggable, and fast
•Great tool for building REST APIs
*
Route Parameter
*
•Representational State Transfer
•HTTP request -> JSON HTTP response
•Business logic on top of MongoDB schemas
• Access control, emails, analytics, etc.
*
*
Find Category
Error handling
Output JSON
*
*
*
•Recall cart is an array of products
*
Get cart from HTTP request
Overwrite user’s cart
Let mongoose handle
casting and validating data
*
•Mongoose lets you be lazy
•Access control using subdocs
*
Create a Stripe charge with the npm ‘stripe’ module
*
Error handling
Empty user cart
on success
*
•Express REST API on top of mongoose
• Access control
• Business logic
• Define what operations user can take on database
•Mongoose casting and validation for APIs
*
•Building an API is tricky
•Lots of different error conditions
•Express has a lot of magic under the hood
*
•Node.js runs in an event loop
•Single threaded
•Can run client and server on same thread!
• Client sends HTTP request
• Client registers a callback awaiting the result
• Server’s “on HTTP request” event handler is triggered
• Server sends response, continues waiting for events
• Client’s callback gets fired
•Test server end-to-end
*
•NodeJS HTTP client
•Isomorphic: runs in both browser and NodeJS
•Same author as Express
*
•Testing Framework for NodeJS
•Same author as Express
•BDD-style syntax
• describe() -> test suite
• it() -> individual test
*
*
*
•NodeJS concurrency makes testing easy
•Not just unit tests - full E2E for your REST API
•Can manipulate database and make arbitrary HTTP
requests
*
•Upcoming EdX Video Course
•Slides on http://www.slideshare.net/vkarpov15
•Looking for beta testers! Sign up for notifications
• http://goo.gl/forms/0ckaJ4YvJN
•Interested in learning about AngularJS?
• Professional AngularJS on Amazon
•More NodeJS+MongoDB content at:
• www.thecodebarbarian.com
• Twitter: @code_barbarian
MongoDB MEAN Stack Webinar October 7, 2015

More Related Content

What's hot (20)

PDF
Hello world - intro to node js
Refresh Annapolis Valley
 
KEY
SGCE 2012 Lightning Talk-Single Page Interface
Domingo Suarez Torres
 
PPTX
Testing your Single Page Application
Wekoslav Stefanovski
 
PDF
JSFoo-2017 Takeaways
Mir Ali
 
PPTX
Meanstack overview
Adthasid Sabmake
 
PPTX
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
Irfan Maulana
 
PDF
Node js projects
💾 Radek Fabisiak
 
PPT
Next generation frontend tooling
pksjce
 
PDF
The Dark Side of Single Page Applications
Dor Kalev
 
PPTX
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?
MarkupBox
 
PPTX
PHP Indonesia - Nodejs Web Development
Irfan Maulana
 
PDF
The MEAN Stack
Md. Ziaul Haq
 
PPTX
MEAN Stack
RoshanTak1
 
PPTX
Codegen2021 blazor mobile
Jose Javier Columbie
 
PPTX
Introduction to mean stack
Praveen Gubbala
 
PDF
Building a Multithreaded Web-Based Game Engine Using HTML5/CSS3 and JavaScrip...
Corey Clark, Ph.D.
 
PDF
Afrimadoni the power of docker
PHP Indonesia
 
PPTX
MEAN Stack - Introduction & Advantages - Why should you switch to MEAN stack ...
Hariharan Ganesan
 
PDF
Node.js primer
Swapnil Mishra
 
PDF
Bringing The Sexy Back To WebWorkers
Corey Clark, Ph.D.
 
Hello world - intro to node js
Refresh Annapolis Valley
 
SGCE 2012 Lightning Talk-Single Page Interface
Domingo Suarez Torres
 
Testing your Single Page Application
Wekoslav Stefanovski
 
JSFoo-2017 Takeaways
Mir Ali
 
Meanstack overview
Adthasid Sabmake
 
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
Irfan Maulana
 
Node js projects
💾 Radek Fabisiak
 
Next generation frontend tooling
pksjce
 
The Dark Side of Single Page Applications
Dor Kalev
 
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?
MarkupBox
 
PHP Indonesia - Nodejs Web Development
Irfan Maulana
 
The MEAN Stack
Md. Ziaul Haq
 
MEAN Stack
RoshanTak1
 
Codegen2021 blazor mobile
Jose Javier Columbie
 
Introduction to mean stack
Praveen Gubbala
 
Building a Multithreaded Web-Based Game Engine Using HTML5/CSS3 and JavaScrip...
Corey Clark, Ph.D.
 
Afrimadoni the power of docker
PHP Indonesia
 
MEAN Stack - Introduction & Advantages - Why should you switch to MEAN stack ...
Hariharan Ganesan
 
Node.js primer
Swapnil Mishra
 
Bringing The Sexy Back To WebWorkers
Corey Clark, Ph.D.
 

Similar to MongoDB MEAN Stack Webinar October 7, 2015 (20)

PPTX
Webinar: Get Started with the MEAN Stack
MongoDB
 
PDF
Developing and Testing a MongoDB and Node.js REST API
All Things Open
 
PPTX
Develop a Basic REST API from Scratch Using TDD with Val Karpov
MongoDB
 
PPTX
Develop a Basic REST API from Scratch Using TDD with Val Karpov
MongoDB
 
PDF
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Mark Leusink
 
PPTX
Tech io spa_angularjs_20130814_v0.9.5
Ganesh Kondal
 
PDF
CMPE282_009994036_PROJECT_REPORT
Sandyarathi Das
 
PDF
MongoDB at Gilt Groupe
MongoDB
 
PPTX
SignalR with ASP.NET MVC 6
Tung Nguyen Thanh
 
PDF
MongoDB
Serdar Buyuktemiz
 
PPTX
An Introduction to MongoDB Compass
MongoDB
 
PDF
JS-IL: Getting MEAN in 1 Hour
Valeri Karpov
 
KEY
20120306 dublin js
Richard Rodger
 
PDF
Mongodb at-gilt-groupe-seattle-2012-09-14-final
MongoDB
 
PPTX
Novedades de MongoDB 3.6
MongoDB
 
PPT
Mongo DB at Community Engine
Community Engine
 
PPT
MongoDB at community engine
mathraq
 
PPTX
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
tilejak773
 
PPTX
Swagger - Making REST APIs friendlier
Miroslav Resetar
 
PDF
Headless cms architecture
Kevin Wenger
 
Webinar: Get Started with the MEAN Stack
MongoDB
 
Developing and Testing a MongoDB and Node.js REST API
All Things Open
 
Develop a Basic REST API from Scratch Using TDD with Val Karpov
MongoDB
 
Develop a Basic REST API from Scratch Using TDD with Val Karpov
MongoDB
 
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Mark Leusink
 
Tech io spa_angularjs_20130814_v0.9.5
Ganesh Kondal
 
CMPE282_009994036_PROJECT_REPORT
Sandyarathi Das
 
MongoDB at Gilt Groupe
MongoDB
 
SignalR with ASP.NET MVC 6
Tung Nguyen Thanh
 
An Introduction to MongoDB Compass
MongoDB
 
JS-IL: Getting MEAN in 1 Hour
Valeri Karpov
 
20120306 dublin js
Richard Rodger
 
Mongodb at-gilt-groupe-seattle-2012-09-14-final
MongoDB
 
Novedades de MongoDB 3.6
MongoDB
 
Mongo DB at Community Engine
Community Engine
 
MongoDB at community engine
mathraq
 
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
tilejak773
 
Swagger - Making REST APIs friendlier
Miroslav Resetar
 
Headless cms architecture
Kevin Wenger
 
Ad

More from Valeri Karpov (17)

PDF
A Practical Introduction to GeoJSON
Valeri Karpov
 
PDF
A Practical Introduction to Functions-as-a-Service
Valeri Karpov
 
PDF
A Gentle Introduction to Functions-as-a-Service
Valeri Karpov
 
PDF
Introducing Async/Await
Valeri Karpov
 
PDF
TAO and the Essence of Modern JavaScript
Valeri Karpov
 
PDF
Mastering Async/Await in JavaScript
Valeri Karpov
 
PDF
React, Redux, and Archetype
Valeri Karpov
 
PDF
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
Valeri Karpov
 
PDF
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Valeri Karpov
 
PDF
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
Valeri Karpov
 
PDF
MongoDB API Talk @ HackPrinceton
Valeri Karpov
 
PDF
MongoDB Israel June Meetup
Valeri Karpov
 
PDF
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
Valeri Karpov
 
PDF
MongoDB: Queries and Aggregation Framework with NBA Game Data
Valeri Karpov
 
PDF
Mongo db in 3 minutes BoilerMake
Valeri Karpov
 
PDF
NodeSummit - MEAN Stack
Valeri Karpov
 
PDF
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
Valeri Karpov
 
A Practical Introduction to GeoJSON
Valeri Karpov
 
A Practical Introduction to Functions-as-a-Service
Valeri Karpov
 
A Gentle Introduction to Functions-as-a-Service
Valeri Karpov
 
Introducing Async/Await
Valeri Karpov
 
TAO and the Essence of Modern JavaScript
Valeri Karpov
 
Mastering Async/Await in JavaScript
Valeri Karpov
 
React, Redux, and Archetype
Valeri Karpov
 
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
Valeri Karpov
 
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Valeri Karpov
 
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
Valeri Karpov
 
MongoDB API Talk @ HackPrinceton
Valeri Karpov
 
MongoDB Israel June Meetup
Valeri Karpov
 
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
Valeri Karpov
 
MongoDB: Queries and Aggregation Framework with NBA Game Data
Valeri Karpov
 
Mongo db in 3 minutes BoilerMake
Valeri Karpov
 
NodeSummit - MEAN Stack
Valeri Karpov
 
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
Valeri Karpov
 
Ad

Recently uploaded (20)

PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 

MongoDB MEAN Stack Webinar October 7, 2015

  • 1. Valeri Karpov Node.js Engineer, MongoDB www.thecodebarbarian.com github.com/vkarpov15 @code_barbarian
  • 2. * •MongoDB is great for storing web/mobile app data •So let’s build a REST API using Node.js! •+ learn a bit about test-driven dev with Node.js •+ learn two MongoDB schema design principles •Server-side only - just JSON, no HTML •No AngularJS part, focus more on MongoDB •Upcoming EdX course
  • 3. * •Part 1: Shopping Cart Application • Search for products • Add them to your cart • Check out with Stripe •Part 2: Using the Mongoose ODM •Part 3: Schema Design •Part 4: Building an API with the Express framework •Part 5: Testing with Mocha + Superagent
  • 4. *
  • 5. *
  • 6. *
  • 7. * •"Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Linus Torvalds •3 schemas for 3 collections: •Products •Categories •Users
  • 8. * •Product belongs to one or more categories •Users can have multiple products in their cart •Representing relationships in MongoDB is tricky •But that’s what mongoose is for
  • 9. * •“Object document mapper” (like ORM, but for MongoDB) •“MongoDB object modeling designed to work in an asynchronous environment” •Written for Node.js •Provides schema validation, pseudo-JOINs, etc.
  • 11. * Register event handler In node.js, you don’t execute I/O imperatively. You register a callback to execute when the I/O is done Prints before “done reading”
  • 13. * Create User Save user to MongoDB Load user from MongoDB Print user to stdout
  • 14. * •Mongoose provides several neat features • Model part of MVC • Default values • Schema validation and declarative schema design
  • 15. * •3 schemas: • Product • Category • User •Going to use mongoose to define schemas •Will use a couple key schema design principles
  • 16. *
  • 17. *
  • 18. *
  • 19. * •What categories are descendants of “Electronics”? • • •What categories are children of “Non-Fiction”? • •What categories are ancestors of “Phones”?
  • 20. *
  • 21. * •Queries in MongoDB should be simple •Strive for minimal data transformation by server •“Store what you query for” •“If you need [the aggregation framework in a heavily used API endpoint], you're screwed anyway, and should fix your program.” - Linus Torvalds •Good for performance and developer sanity
  • 22. * User’s cart as an array of ObjectIds...
  • 23. * •Product and user = many-to-many relationship •Don’t necessarily need a mapping table •User won’t have 1000s of products in cart •Can represent relationship as array in user since one side is small •If one side of many-to-many is bounded and/or small, it is a good candidate for embedding •Arrays that grow without bound are an antipattern! • 16mb document size limit • network overhead
  • 24. * •Most popular Node.js web framework •Simple, pluggable, and fast •Great tool for building REST APIs
  • 26. * •Representational State Transfer •HTTP request -> JSON HTTP response •Business logic on top of MongoDB schemas • Access control, emails, analytics, etc.
  • 27. *
  • 29. *
  • 30. *
  • 31. * •Recall cart is an array of products
  • 32. * Get cart from HTTP request Overwrite user’s cart Let mongoose handle casting and validating data
  • 33. * •Mongoose lets you be lazy •Access control using subdocs
  • 34. * Create a Stripe charge with the npm ‘stripe’ module
  • 35. * Error handling Empty user cart on success
  • 36. * •Express REST API on top of mongoose • Access control • Business logic • Define what operations user can take on database •Mongoose casting and validation for APIs
  • 37. * •Building an API is tricky •Lots of different error conditions •Express has a lot of magic under the hood
  • 38. * •Node.js runs in an event loop •Single threaded •Can run client and server on same thread! • Client sends HTTP request • Client registers a callback awaiting the result • Server’s “on HTTP request” event handler is triggered • Server sends response, continues waiting for events • Client’s callback gets fired •Test server end-to-end
  • 39. * •NodeJS HTTP client •Isomorphic: runs in both browser and NodeJS •Same author as Express
  • 40. * •Testing Framework for NodeJS •Same author as Express •BDD-style syntax • describe() -> test suite • it() -> individual test
  • 41. *
  • 42. *
  • 43. * •NodeJS concurrency makes testing easy •Not just unit tests - full E2E for your REST API •Can manipulate database and make arbitrary HTTP requests
  • 44. * •Upcoming EdX Video Course •Slides on http://www.slideshare.net/vkarpov15 •Looking for beta testers! Sign up for notifications • http://goo.gl/forms/0ckaJ4YvJN •Interested in learning about AngularJS? • Professional AngularJS on Amazon •More NodeJS+MongoDB content at: • www.thecodebarbarian.com • Twitter: @code_barbarian