MongoDB Israel June Meetup
-
Upload
valeri-karpov -
Category
Technology
-
view
421 -
download
3
description
Transcript of MongoDB Israel June Meetup
MongoDB 2.6 Features in NodeJS Web Apps
Valeri KarpovSoftware Engineer, MongoDBwww.thecodebarbarian.com
www.slideshare.net/vkarpov15github.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