Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MongoDB Israel June Meetup

783 views

Published on

An overview of using new features in MongoDB 2.6 in the context of NodeJS web apps

Published in: Technology
  • Be the first to comment

MongoDB Israel June Meetup

  1. 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. 2. * Who Am I? •CI/NodeJS Engineer at MongoDB •Maintainer of mongoose ODM •Blogs at thecodebarbarian.com
  3. 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. 4. * Additional Tools •MongoDB NodeJS Driver •Mongoose: ODM for NodeJS, convenience wrapper around mongodb driver •Express: lightweight server MVC for NodeJS
  5. 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. 6. * The App We’ll Be Using •Open source food journal, LeanMEAN •Counts calories, computes ratios, etc. •MEAN = MongoDB, ExpressJS, AngularJS, NodeJS
  7. 7. * The App We’ll Be Using - Demo
  8. 8. * App Overview •3 models: FoodItem, User, Day •Day = list of FoodItems a User ate on a given date
  9. 9. * What a FoodItem looks like •From USDA SR-25 data set •FoodItem: description, list of nutrients and weights
  10. 10. * Food Item Nutrients
  11. 11. * Food Item Weights
  12. 12. * Simple SR-25 Query •How many carbs in 1 serving of raw kale?
  13. 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. 14. * Example of Text Search in Shell •Top 3 results for “grass-fed beef”
  15. 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. 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. 17. * Use Case For Text Search •Autocomplete/typeahead for entering food items
  18. 18. * The API Endpoint in Express •All our API methods:
  19. 19. * Search API Implementation Note: text search API is atypical, docs here
  20. 20. * A Note on $meta and Text Score
  21. 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. 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. 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. 24. * Using $min and $max in NodeJS •No known restrictions •Still recommend • node-mongodb-native >= 1.4.0 • mongoose >= 3.8.0
  25. 25. * Taking a Look at the User Schema
  26. 26. * $min in Action with Mongoose
  27. 27. * Notes about $min and $max •$min and $max use BSON comparison order •Can have unexpected results! •Example: null < 4 • • •
  28. 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. 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. 30. * Using $out from the Shell •$out is pipeline stage, like $group, $match, etc. •Takes string input: name of collection
  31. 31. * Using $out from the Shell: Data
  32. 32. * Running $out from the Shell
  33. 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. 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. 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. 36. * The Actual Aggregation, 1-3 •6 stages + $out
  37. 37. * The Actual Aggregation, 4-5
  38. 38. * The Actual Aggregation, 6-7
  39. 39. * Accessing via API Endpoint
  40. 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. 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

×