SlideShare a Scribd company logo
MongoDB 2.6 Features in NodeJS Web
Apps
Valeri Karpov
Software Engineer, MongoDB
www.thecodebarbarian.com
www.slideshare.net/vkarpov15
github.com/vkarpov15
@code_barbarian
*
Who Am I?
•CI/NodeJS Engineer at MongoDB
•Maintainer of mongoose ODM
•Blogs at thecodebarbarian.com
*
Talk Overview
•MongoDB 2.6 released in April (currently 2.6.2)
•Exciting new features including:
• Text search
• $min and $max operators for updates
• $out operator for aggregation
•Examples of use cases in NodeJS Single Page App
*
Additional Tools
•MongoDB NodeJS Driver
•Mongoose: ODM for NodeJS, convenience wrapper
around mongodb driver
•Express: lightweight server MVC for NodeJS
*
More Minor Tools
•Moment: elegant wrapper around clunky JS Date
•Omni: dependency injector for NodeJS
•AngularJS: client-side MVC framework
• Don’t worry if you don’t know AngularJS, won’t be used
much in this presentation
*
The App We’ll Be Using
•Open source food journal, LeanMEAN
•Counts calories, computes ratios, etc.
•MEAN = MongoDB, ExpressJS, AngularJS, NodeJS
*
The App We’ll Be Using - Demo
*
App Overview
•3 models: FoodItem, User, Day
•Day = list of FoodItems a User ate on a given date
*
What a FoodItem looks like
•From USDA SR-25 data set
•FoodItem: description, list of nutrients and weights
*
Food Item Nutrients
*
Food Item Weights
*
Simple SR-25 Query
•How many carbs in 1 serving of raw kale?
*
Part I: Text Search
•Problem: searching for FoodItems
•Don’t want user to have to type exactly “Kale, raw”
•Would be ideal if “raw kale” matched “Kale, raw”
*
Example of Text Search in Shell
•Top 3 results for “grass-fed beef”
*
Text Search Details
•Need to create a text index:
• db.nutrition.ensureIndex({ description :
"text" });
•Need MongoDB server >= 2.6.0
•Can specify language (15 supported languages)
• db.nutrition.ensureIndex({ description :
"text" }, {default_language:"ru"});
• Supports English and Russian, but no Hebrew yet
•Limitation: 1 text search index per collection
*
Text Search In NodeJS
•Recommend using:
• node-mongodb-native >= 1.4.0 (1.2 and 1.3 seem to work)
• mongoose >= 3.8.9
•Since JS, very similar to shell
*
Use Case For Text Search
•Autocomplete/typeahead for entering food items
*
The API Endpoint in Express
•All our API methods:
*
Search API Implementation
Note: text search API is atypical, docs here
*
A Note on $meta and Text Score
*
Part II: $min and $max
•Mongoose users often update docs in memory
•Not necessarily good practice!
•Just use update() where possible
•$min and $max enable new use case for update()
•Not to be confused with aggregation $min/$max
•Not to be confused with query modifier $min/$max
*
Using $min and $max in the Shell
•Conditional update:
• $min: only update if new value < old value
• $max: only update if new value > old value
*
More Interesting Use Case for $min
•$min and $max also work on ISODate
•Common use case: oldest piece of data for user
•In LeanMEAN, user can enter data for past days
•Inexpensive query, but 0 queries better than 1
•Save an HTTP request and a query = win
•Tradeoff: extra load when saving
*
Using $min and $max in NodeJS
•No known restrictions
•Still recommend
• node-mongodb-native >= 1.4.0
• mongoose >= 3.8.0
*
Taking a Look at the User Schema
*
$min in Action with Mongoose
*
Notes about $min and $max
•$min and $max use BSON comparison order
•Can have unexpected results!
•Example: null < 4
•
•
•
*
Notes about $min and $max, cont
•$min and $max use BSON comparison order
•Example: ISODate > 4
Careful to use $min/$max with right type!
*
Part III: Aggregation $out
•Before MongoDB 2.6, aggregation limited to 16MB
•Also no good way to output to collection
•2.6.0 solves both!
•Aggregation returns a cursor, no fixed limit on size
•$out stage outputs agg. results to collection
*
Using $out from the Shell
•$out is pipeline stage, like $group, $match, etc.
•Takes string input: name of collection
*
Using $out from the Shell: Data
*
Running $out from the Shell
*
Using $out in NodeJS
•node-mongodb-native >= 1.4.0 recommended
•mongoose >= 3.8.0 recommended
•But .out() aggregation helper requires >= 3.8.9
*
Use Case: All I Need is One Document
•Ideal MEAN stack world: 1 document ⇔ 1 page
•NodeJS makes scheduling aggregations easy
•Use $out to build complex data sets with ease
*
Average Calories Per Week Per User
•Show week by week average calories per day
•Bulky, complex aggregation
•Don’t want to do this on-demand
•Use node-cron to run aggregation once per day
*
The Actual Aggregation, 1-3
•6 stages + $out
*
The Actual Aggregation, 4-5
*
The Actual Aggregation, 6-7
*
Accessing via API Endpoint
*
Notes on $out
•Always creates a new collection
•Overwrites existing collection, no append mode yet
•Under the hood:
• Puts aggregation output into temp collection
• Atomic rename of collection after complete
•Old data still available during aggregation
•Temp collection inserts go in oplog and replicate
•Can’t $out to a sharded or capped collection
*
Thanks for Listening!
•Slides on:
• Twitter: @code_barbarian
• Slideshare: slideshare.net/vkarpov15
•Repo on github: github.com/vkarpov15/lean-mean-
nutrition
•Blog post on SR-25 data set

More Related Content

PPTX
Simple hack: use multiple mongodb databases in a nodejs express mongodb appli...
PDF
Mongo db in 3 minutes BoilerMake
DOCX
Node js getting started
PPTX
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
PPTX
A slightly advanced introduction to node.js
KEY
MongoDB Command Line Tools
PDF
Create a RESTful API with NodeJS, Express and MongoDB
KEY
MongoDB Strange Loop 2009
Simple hack: use multiple mongodb databases in a nodejs express mongodb appli...
Mongo db in 3 minutes BoilerMake
Node js getting started
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
A slightly advanced introduction to node.js
MongoDB Command Line Tools
Create a RESTful API with NodeJS, Express and MongoDB
MongoDB Strange Loop 2009

What's hot (20)

PDF
MongoDB Mojo: Building a Basic Perl App
PPTX
Introduction to node
PPTX
Nodejs server lesson 3
PPTX
PHP Indonesia - Nodejs Web Development
PPTX
Zero to Hipster with the M.I.K.E. Stack
PPT
Breaking The Cross Domain Barrier
PDF
Rapid API development on MongoDB
KEY
MongoDB at RubyEnRails 2009
PPTX
Stay Out Please
PDF
Flask jwt authentication tutorial
PPTX
Mule webservices
KEY
MongoDB EuroPython 2009
KEY
MongoDB Hadoop DC
PDF
Fine Uploader S3
KEY
Modernizr, Yepnope, and Polyfills
PDF
Node JS Crash Course
PPTX
PPT
JavaScript State of the Union - Jan 2013
PDF
REST Web API with MongoDB
MongoDB Mojo: Building a Basic Perl App
Introduction to node
Nodejs server lesson 3
PHP Indonesia - Nodejs Web Development
Zero to Hipster with the M.I.K.E. Stack
Breaking The Cross Domain Barrier
Rapid API development on MongoDB
MongoDB at RubyEnRails 2009
Stay Out Please
Flask jwt authentication tutorial
Mule webservices
MongoDB EuroPython 2009
MongoDB Hadoop DC
Fine Uploader S3
Modernizr, Yepnope, and Polyfills
Node JS Crash Course
JavaScript State of the Union - Jan 2013
REST Web API with MongoDB
Ad

Viewers also liked (11)

PDF
MEAN Stack Workshop at Node Philly, 4/9/14
PDF
JS-IL: Getting MEAN in 1 Hour
PDF
Conquering AngularJS Limitations
PDF
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
PDF
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
PDF
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
PDF
Lessons in Open Source from the MongooseJS ODM
PDF
MongoDB: Queries and Aggregation Framework with NBA Game Data
PDF
NodeSummit - MEAN Stack
PDF
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
PDF
TDD a REST API With Node.js and MongoDB
MEAN Stack Workshop at Node Philly, 4/9/14
JS-IL: Getting MEAN in 1 Hour
Conquering AngularJS Limitations
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
Lessons in Open Source from the MongooseJS ODM
MongoDB: Queries and Aggregation Framework with NBA Game Data
NodeSummit - MEAN Stack
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
TDD a REST API With Node.js and MongoDB
Ad

Similar to MongoDB Israel June Meetup (20)

PDF
Developing and Testing a MongoDB and Node.js REST API
PDF
MEAN Stack WeNode Barcelona Workshop
PDF
MongoDB MEAN Stack Webinar October 7, 2015
PPTX
Webinar: Get Started with the MEAN Stack
PDF
MongoDB at Gilt Groupe
PDF
Mongodb at-gilt-groupe-seattle-2012-09-14-final
PPT
MongoDB Pros and Cons
PDF
PDF
Cloud App Develop
PDF
Gotszling mogo db-membase
PPTX
Mongo db nosql (1)
PPTX
Develop a Basic REST API from Scratch Using TDD with Val Karpov
PPTX
Develop a Basic REST API from Scratch Using TDD with Val Karpov
KEY
PDF
MongoDB and Ruby on Rails
PPTX
Basics of MongoDB
PDF
How do i Meet MongoDB
KEY
Building a Mongo DSL in Scala at Hot Potato (Lincoln Hochberg)
PDF
MongoDB: a gentle, friendly overview
PDF
Mongo db first steps with csharp
Developing and Testing a MongoDB and Node.js REST API
MEAN Stack WeNode Barcelona Workshop
MongoDB MEAN Stack Webinar October 7, 2015
Webinar: Get Started with the MEAN Stack
MongoDB at Gilt Groupe
Mongodb at-gilt-groupe-seattle-2012-09-14-final
MongoDB Pros and Cons
Cloud App Develop
Gotszling mogo db-membase
Mongo db nosql (1)
Develop a Basic REST API from Scratch Using TDD with Val Karpov
Develop a Basic REST API from Scratch Using TDD with Val Karpov
MongoDB and Ruby on Rails
Basics of MongoDB
How do i Meet MongoDB
Building a Mongo DSL in Scala at Hot Potato (Lincoln Hochberg)
MongoDB: a gentle, friendly overview
Mongo db first steps with csharp

More from Valeri Karpov (10)

PDF
A Practical Introduction to GeoJSON
PDF
A Practical Introduction to Functions-as-a-Service
PDF
A Gentle Introduction to Functions-as-a-Service
PDF
Introducing Async/Await
PDF
TAO and the Essence of Modern JavaScript
PDF
Mastering Async/Await in JavaScript
PDF
React, Redux, and Archetype
PDF
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
PDF
MongoDB API Talk @ HackPrinceton
PDF
MEAN Stack - Google Developers Live 10/03/2013
A Practical Introduction to GeoJSON
A Practical Introduction to Functions-as-a-Service
A Gentle Introduction to Functions-as-a-Service
Introducing Async/Await
TAO and the Essence of Modern JavaScript
Mastering Async/Await in JavaScript
React, Redux, and Archetype
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
MongoDB API Talk @ HackPrinceton
MEAN Stack - Google Developers Live 10/03/2013

Recently uploaded (20)

PDF
Architecture types and enterprise applications.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Developing a website for English-speaking practice to English as a foreign la...
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
observCloud-Native Containerability and monitoring.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Architecture types and enterprise applications.pdf
Web App vs Mobile App What Should You Build First.pdf
TLE Review Electricity (Electricity).pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Developing a website for English-speaking practice to English as a foreign la...
Module 1.ppt Iot fundamentals and Architecture
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
A comparative study of natural language inference in Swahili using monolingua...
A contest of sentiment analysis: k-nearest neighbor versus neural network
observCloud-Native Containerability and monitoring.pptx
cloud_computing_Infrastucture_as_cloud_p
Univ-Connecticut-ChatGPT-Presentaion.pdf
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Hindi spoken digit analysis for native and non-native speakers
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...

MongoDB Israel June Meetup

  • 1. MongoDB 2.6 Features in NodeJS Web Apps Valeri Karpov Software Engineer, MongoDB www.thecodebarbarian.com www.slideshare.net/vkarpov15 github.com/vkarpov15 @code_barbarian
  • 2. * Who Am I? •CI/NodeJS Engineer at MongoDB •Maintainer of mongoose ODM •Blogs at thecodebarbarian.com
  • 3. * Talk Overview •MongoDB 2.6 released in April (currently 2.6.2) •Exciting new features including: • Text search • $min and $max operators for updates • $out operator for aggregation •Examples of use cases in NodeJS Single Page App
  • 4. * Additional Tools •MongoDB NodeJS Driver •Mongoose: ODM for NodeJS, convenience wrapper around mongodb driver •Express: lightweight server MVC for NodeJS
  • 5. * More Minor Tools •Moment: elegant wrapper around clunky JS Date •Omni: dependency injector for NodeJS •AngularJS: client-side MVC framework • Don’t worry if you don’t know AngularJS, won’t be used much in this presentation
  • 6. * The App We’ll Be Using •Open source food journal, LeanMEAN •Counts calories, computes ratios, etc. •MEAN = MongoDB, ExpressJS, AngularJS, NodeJS
  • 7. * The App We’ll Be Using - Demo
  • 8. * App Overview •3 models: FoodItem, User, Day •Day = list of FoodItems a User ate on a given date
  • 9. * What a FoodItem looks like •From USDA SR-25 data set •FoodItem: description, list of nutrients and weights
  • 12. * Simple SR-25 Query •How many carbs in 1 serving of raw kale?
  • 13. * Part I: Text Search •Problem: searching for FoodItems •Don’t want user to have to type exactly “Kale, raw” •Would be ideal if “raw kale” matched “Kale, raw”
  • 14. * Example of Text Search in Shell •Top 3 results for “grass-fed beef”
  • 15. * Text Search Details •Need to create a text index: • db.nutrition.ensureIndex({ description : "text" }); •Need MongoDB server >= 2.6.0 •Can specify language (15 supported languages) • db.nutrition.ensureIndex({ description : "text" }, {default_language:"ru"}); • Supports English and Russian, but no Hebrew yet •Limitation: 1 text search index per collection
  • 16. * Text Search In NodeJS •Recommend using: • node-mongodb-native >= 1.4.0 (1.2 and 1.3 seem to work) • mongoose >= 3.8.9 •Since JS, very similar to shell
  • 17. * Use Case For Text Search •Autocomplete/typeahead for entering food items
  • 18. * The API Endpoint in Express •All our API methods:
  • 19. * Search API Implementation Note: text search API is atypical, docs here
  • 20. * A Note on $meta and Text Score
  • 21. * Part II: $min and $max •Mongoose users often update docs in memory •Not necessarily good practice! •Just use update() where possible •$min and $max enable new use case for update() •Not to be confused with aggregation $min/$max •Not to be confused with query modifier $min/$max
  • 22. * Using $min and $max in the Shell •Conditional update: • $min: only update if new value < old value • $max: only update if new value > old value
  • 23. * More Interesting Use Case for $min •$min and $max also work on ISODate •Common use case: oldest piece of data for user •In LeanMEAN, user can enter data for past days •Inexpensive query, but 0 queries better than 1 •Save an HTTP request and a query = win •Tradeoff: extra load when saving
  • 24. * Using $min and $max in NodeJS •No known restrictions •Still recommend • node-mongodb-native >= 1.4.0 • mongoose >= 3.8.0
  • 25. * Taking a Look at the User Schema
  • 26. * $min in Action with Mongoose
  • 27. * Notes about $min and $max •$min and $max use BSON comparison order •Can have unexpected results! •Example: null < 4 • • •
  • 28. * Notes about $min and $max, cont •$min and $max use BSON comparison order •Example: ISODate > 4 Careful to use $min/$max with right type!
  • 29. * Part III: Aggregation $out •Before MongoDB 2.6, aggregation limited to 16MB •Also no good way to output to collection •2.6.0 solves both! •Aggregation returns a cursor, no fixed limit on size •$out stage outputs agg. results to collection
  • 30. * Using $out from the Shell •$out is pipeline stage, like $group, $match, etc. •Takes string input: name of collection
  • 31. * Using $out from the Shell: Data
  • 32. * Running $out from the Shell
  • 33. * Using $out in NodeJS •node-mongodb-native >= 1.4.0 recommended •mongoose >= 3.8.0 recommended •But .out() aggregation helper requires >= 3.8.9
  • 34. * Use Case: All I Need is One Document •Ideal MEAN stack world: 1 document ⇔ 1 page •NodeJS makes scheduling aggregations easy •Use $out to build complex data sets with ease
  • 35. * Average Calories Per Week Per User •Show week by week average calories per day •Bulky, complex aggregation •Don’t want to do this on-demand •Use node-cron to run aggregation once per day
  • 36. * The Actual Aggregation, 1-3 •6 stages + $out
  • 40. * Notes on $out •Always creates a new collection •Overwrites existing collection, no append mode yet •Under the hood: • Puts aggregation output into temp collection • Atomic rename of collection after complete •Old data still available during aggregation •Temp collection inserts go in oplog and replicate •Can’t $out to a sharded or capped collection
  • 41. * Thanks for Listening! •Slides on: • Twitter: @code_barbarian • Slideshare: slideshare.net/vkarpov15 •Repo on github: github.com/vkarpov15/lean-mean- nutrition •Blog post on SR-25 data set