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.

Mongoose v3 :: The Future is Bright

30,838 views

Published on

See whats ahead in Mongoose v3

Published in: Technology, Sports
  • While I can see that space delimited fields in a query is nice an concise, I think it will limit your future features. e.g. How will you support functions in query in the future? What about nested subqueries? From where I'm standing (as a Mongoose newbie but an experienced SQL dev) stuffing all the field names inside a single text string appear to points to rather limited expressability in the future.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Super excited about this!

    Honestly, my biggest frustration with Mongoose has always been that the documentation was about as sparse as you could possibly get. When you're writing up documentation for the new version, PLEASE PLEASE PLEASE try and think of various applications and cases for each part of the documentation, not just one canonical example like exists now.

    For example, even in your slide talking about new ways of sorting, you only showed the example for ascending, not descending - which I'm sure would take some kind of modifier. This is an obvious one, but I would just encourage the Mongoose people to put a little more effort into showing multiple examples - they never hurt, only help.

    Thanks so much for working on this wonderful software that I use every day!
    Justin Schier
    @justinschier
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Like it :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Mongoose v3 :: The Future is Bright

  1. 1. MONGOOSE V3 Looking ahead jsconf.ar 2012 @aaronheckmann
  2. 2. MONGOOSE V3
  3. 3. MONGOOSE V3•What’s wrong
  4. 4. MONGOOSE V3•What’s wrong•The Fix
  5. 5. MONGOOSE V3•What’s wrong•The Fix•Improvements
  6. 6. MONGOOSE V3•What’s wrong•The Fix•Improvements•Support
  7. 7. MONGOOSE V3 The Future Is Bright
  8. 8. MONGOOSE V3What’s Wrong?
  9. 9. MONGOOSE V3Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases arent special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless youre Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, its a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- lets do more of those! -- Tim Peters, The Zen of Python http://www.python.org/dev/peps/pep-0020/
  10. 10. MONGOOSE V3Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense. There should be one--Readability counts.Special cases arent special enough to break the rules.Although practicality beats purity. and preferably only oneErrors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it. --obvious way to do it.Although that way may not be obvious at first unless youre Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, its a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- lets do more of those! -- Tim Peters, The Zen of Python http://www.python.org/dev/peps/pep-0020/
  11. 11. MONGOOSE V3One way
  12. 12. MONGOOSE V3One way User.where(‘age’).$gt(17).select(‘name’).exec(cb) User.where(‘age’).gt(17).select({‘name’: 1}).exec(cb) User.where(‘age’).gt(17).select(‘name’, 1).run(cb) User.find({ age: { $gt: 17 }}).select([‘name’]).exec(cb) User.find({ age: { $gt: 17 }}, [‘name’]).run(cb)
  13. 13. MONGOOSE V3One way User.where(‘age’).$gt(17).select(‘name’).exec(cb) User.where(‘age’).gt(17).select({‘name’: 1}).exec(cb) User.where(‘age’).gt(17).select(‘name’, 1).run(cb) User.find({ age: { $gt: 17 }}).select([‘name’]).exec(cb) User.find({ age: { $gt: 17 }}, [‘name’]).run(cb)
  14. 14. MONGOOSE V3One way User.where(‘age’).$gt(17).select(‘name’).exec(cb) User.where(‘age’).gt(17).select({‘name’: 1}).exec(cb) User.where(‘age’).gt(17).select(‘name’, 1).run(cb) User.find({ age: { $gt: 17 }}).select([‘name’]).exec(cb) User.find({ age: { $gt: 17 }}, [‘name’]).run(cb)
  15. 15. MONGOOSE V3One way User.where(‘age’).$gt(17).select(‘name’).exec(cb) User.where(‘age’).gt(17).select({‘name’: 1}).exec(cb) User.where(‘age’).gt(17).select(‘name’, 1).run(cb) User.find({ age: { $gt: 17 }}).select([‘name’]).exec(cb) User.find({ age: { $gt: 17 }}, [‘name’]).run(cb)
  16. 16. MONGOOSE V3One way User.where(‘age’).$gt(17).select(‘name’).exec(cb) User.where(‘age’).gt(17).select({‘name’: 1}).exec(cb) User.where(‘age’).gt(17).select(‘name’, 1).run(cb) User.find({ age: { $gt: 17 }}).select([‘name’]).exec(cb) User.find({ age: { $gt: 17 }}, [‘name’]).run(cb)
  17. 17. MONGOOSE V3http://www.motifake.com/image/demotivational-poster/1106/determination-determination-faceplant-demotivational-posters-1307398930.jpg
  18. 18. MONGOOSE V3API pollution Query methods$or -> or $in -> in $near -> near$nor -> nor $nin -> nin $within -> within$gt -> gt $all -> all $box -> box$gte -> gte $regex ->regex $center - > center$lt -> lt $size -> size $centerSphere -> centerSphere$lte -> lte $slice -> slice $maxDistance ->maxDistance
  19. 19. MONGOOSE V3API pollution Query methods$ne -> ne only -> select({ path: 1 })$mod -> mod exclude -> select({ path: 0 })$exists -> exists run -> exec$elemMatch -> elemMatch notEqualTo -> neasc -> sort({ path: 1 }) wherein -> withindesc -> sort({ path: 0}) fields -> select
  20. 20. MONGOOSE V3API pollution Connection methodscreateConnectioncreateSetConnectionconnectconnectSet
  21. 21. MONGOOSE V3The Fix?createConnection $nin -> nin $ne -> necreateSetConnection $all -> all $mod -> modconnect $regex -> regex $exists -> existsconnectSet $size -> size $elemMatch -> elemMatch$or -> or $slice -> slice asc -> sort({ path: 1 })$nor -> nor $near -> near desc -> sort({ path: 0})$gt -> gt $within -> within only -> select({ path: 1 })$gte -> gte $box -> box exclude -> select({ path: 0 })$lt -> lt $center -> center run -> exec$lte -> lte $centerSphere -> centerSphere notEqualTo -> ne$in -> in $maxDistance -> maxDistance wherein -> within fields -> select
  22. 22. MONGOOSE V3The Fix kill all the thingscreateConnection $nin -> nin $ne -> necreateSetConnection $all -> all $mod -> modconnect $regex -> regex $exists -> existsconnectSet $size -> size $elemMatch -> elemMatch$or -> or $slice -> slice asc -> sort({ path: 1 })$nor -> nor $near -> near desc -> sort({ path: 0})$gt -> gt $within -> within only -> select({ path: 1 })$gte -> gte $box -> box exclude -> select({ path: 0 })$lt -> lt $center -> center run -> exec$lte -> lte $centerSphere -> centerSphere notEqualTo -> ne$in -> in $maxDistance -> maxDistance wherein -> within fields -> select
  23. 23. MONGOOSE V3The Fix kill all the thingscreateConnection $nin -> nin $ne -> necreateSetConnection $all -> all $mod -> modconnect $regex -> regex $exists -> existsconnectSet $size -> size $elemMatch -> elemMatch$or -> or $slice -> slice asc -> sort({ path: 1 })$nor -> nor $near -> near desc -> sort({ path: 0})$gt -> gt $within -> within only -> select({ path: 1 })$gte -> gte $box -> box exclude -> select({ path: 0 })$lt -> lt $center -> center run -> exec$lte -> lte $centerSphere -> centerSphere notEqualTo -> ne$in -> in $maxDistance -> maxDistance wherein -> within fields -> select http://soundbible.com/1575-High-Definition-Machine-Gun.html http://openclipart.org/detail/20561/set-of-bullet-holes-by-rg1024
  24. 24. MONGOOSE V3The Fix remove query aliasesCoffee.where(‘flavor’).$in(array).run()
  25. 25. MONGOOSE V3The Fix remove query aliasesCoffee.where(‘flavor’).$in(array).run() Coffee.where(‘flavor’).in(array).exec()
  26. 26. MONGOOSE V3The Fix remove query aliasesCoffee.where(‘flavor’).$in(array).run() Coffee.where(‘flavor’).in(array).exec()
  27. 27. MONGOOSE V3The Fix remove query aliasesCoffee.find().asc(‘flavor’).run()
  28. 28. MONGOOSE V3The Fix remove query aliasesCoffee.find().asc(‘flavor’).run() Coffee.find().sort(‘flavor’).exec()
  29. 29. MONGOOSE V3The Fix remove query aliasesCoffee.find().asc(‘flavor’).run() Coffee.find().sort(‘flavor’).exec()
  30. 30. MONGOOSE V3The Fix Combine connection methodsgoose.createSetConnection(connString)
  31. 31. MONGOOSE V3The Fix Combine connection methodsgoose.createSetConnection(connString) goose.createConnection(connString)
  32. 32. MONGOOSE V3The Fix Combine connection methodsmongoose.connectSet(connString)
  33. 33. MONGOOSE V3The Fix Combine connection methodsmongoose.connectSet(connString) mongoose.connect(connString)
  34. 34. MONGOOSE V3What’s Wrong?
  35. 35. MONGOOSE V3What’s Wrong? Query method arguments
  36. 36. MONGOOSE V3What’s Wrong? Query method argumentsquery.select(array)query.select(‘path’, 1, ‘path’, 1)query.select({ key: 0 })query.select(‘space delimited’)query.hint(‘path’, 1, ‘path’, 1)query.hint({ key: 1})
  37. 37. MONGOOSE V3What’s Wrong? Query method argumentsquery.select(array)query.select(‘path’, 1, ‘path’, 1)query.select({ key: 0 })query.select(‘space delimited’)query.hint(‘path’, 1, ‘path’, 1)query.hint({ key: 1})
  38. 38. MONGOOSE V3The Fix Query method argumentsquery.select(‘include -exclude’)
  39. 39. MONGOOSE V3The Fix Query method argumentsquery.select(‘include -exclude’)query.select({ include: 1, exclude: 0 })
  40. 40. MONGOOSE V3The Fix Query method argumentsquery.select({ include: 1, exclude: 0 })query.select(‘include -exclude’)query.sort({ include: 1, exclude: 0 })query.sort(‘include -exclude’)
  41. 41. MONGOOSE V3Missing MongoDB features
  42. 42. MONGOOSE V3Missing MongoDB features
  43. 43. MONGOOSE V3Missing MongoDB features
  44. 44. MONGOOSE V3Missing MongoDB features•Map Reduce
  45. 45. MONGOOSE V3Missing MongoDB features•Map Reduce var o = { map: mapFn, reduce: rFn } Talks.mapReduce(o, function (err, res) { console.log(res) })
  46. 46. MONGOOSE V3Missing MongoDB features•Map Reduce var o = { map: mapFn, reduce: rFn } o.out = { replace: ‘mapped_talks’ } Talks.mapReduce(o, function (err, res) { res.find().where().exec(function (err, docs) { console.log(res) }) })
  47. 47. MONGOOSE V3Missing MongoDB features•FindAndModify
  48. 48. MONGOOSE V3Missing MongoDB features•FindAndModify // old way var o = { _id: anObjectId } Talks.findOne(o, function (err, doc) { doc.name = ‘peanut butter’; doc.save() })
  49. 49. MONGOOSE V3Missing MongoDB features•FindAndModify // new way var o = { _id: anObjectId } , up = { $set: { name: ‘peanut butter’}} Talks.findOneAndUpdate(o, up, function (err, doc) { console.log(doc) })
  50. 50. MONGOOSE V3Missing MongoDB features•FindAndModify var o = { name: ‘peanut butter’ } Talks.findOneAndRemove(o, function (err, res) { console.log(res) })
  51. 51. MONGOOSE V3Missing MongoDB features•FindAndModify Talks.findByIdAndUpdate(id, update, cb) Talks.findByIdAndRemove(id, update, cb)
  52. 52. MONGOOSE V3Missing MongoDB features•Map Reduce•FindAndModify•Capped collections * * soon!
  53. 53. MONGOOSE V3Missing MongoDB features•Map Reduce•FindAndModify•Capped collections *•Aggregation * * soon!
  54. 54. MONGOOSE V3Missing MongoDB features•Aggregation Article.aggregate( { $project : { author : 1, tags : 1 }} , { $unwind : "$tags" } , { $group : { _id : { tags : 1 } , authors : { $addToSet : "$author" } } }); * soon!
  55. 55. MONGOOSE V3Missing MongoDB features•Aggregation Article.aggregate( { $project : { author : 1, tags : 1 }} , { $unwind : "$tags" } , { $group : { _id : { tags : 1 } , authors : { $addToSet : "$author" } } }); * soon!
  56. 56. MONGOOSE V3Missing MongoDB features•Aggregation Article.aggregate( { $project : { author : 1, tags : 1 }} , { $unwind : "$tags" } , { $group : { _id : { tags : 1 } , authors : { $addToSet : "$author" } } }); * soon!
  57. 57. MONGOOSE V3Missing MongoDB features•Aggregation Article.aggregate( { $project : { author : 1, tags : 1 }} , { $unwind : "$tags" } , { $group : { _id : { tags : 1 } , authors : { $addToSet : "$author" } } }); * soon!
  58. 58. MONGOOSE V3Missing MongoDB features•Aggregation Article.aggregate( { $project : { author : 1, tags : 1 }} , { $unwind : "$tags" } , { $group : { _id : { tags : 1 } , authors : { $addToSet : "$author" } } }); * soon!
  59. 59. MONGOOSE V3Missing MongoDB features•Aggregation Article.aggregate( { $project : { author : 1, tags : 1 }} , { $unwind : "$tags" } , { $group : { _id : { tags : 1 } , authors : { $addToSet : "$author" } } }); * soon!
  60. 60. MONGOOSE V3Missing MongoDB features•Map Reduce•FindAndModify•Capped collections *•Aggregation *•Improved geo-spatial support * * soon!
  61. 61. MONGOOSE V3Performance
  62. 62. MONGOOSE V3Performance•lean queries
  63. 63. MONGOOSE V3Performance•lean queries Talks .findOne() .exec(function (err, doc) { // doc.save() })
  64. 64. MONGOOSE V3Performance•lean queries Talks .findOne() .setOptions({ lean: true }) .exec(function (err, doc) { // doc.save })
  65. 65. MONGOOSE V3Performance•lean queries Talks .findOne() .setOptions({ lean: true }) .exec(function (err, doc) { // doc.save -> undefined })
  66. 66. MONGOOSE V3Performance•lean queries•background indexes
  67. 67. MONGOOSE V3Performance•lean queries•background indexes•connection pool size?
  68. 68. MONGOOSE V3Performance•lean queries•background indexes•connection pool size?•memory usage?
  69. 69. MONGOOSE V3General Improvements
  70. 70. MONGOOSE V3General Improvements•Improved update command generation for arrays
  71. 71. MONGOOSE V3General Improvements•Improved update command generation for arrays var schema = new Schema({ tags: [], name: String })
  72. 72. MONGOOSE V3General Improvements•Improved update command generation for arrays var schema = new Schema({ tags: [], name: String }) var Talks = db.model(‘Talk’, schema)
  73. 73. MONGOOSE V3General Improvements•Improved update command generation for arrays var schema = new Schema({ tags: [], name: String }) var Talks = db.model(‘Talk’, schema) Talks.findById(id, function (err, talk) { talk.name = ‘peanut butter’ talk.tags.push(‘nerdcore’) talk.save(callback) })
  74. 74. MONGOOSE V3General Improvements•Improved update command generation for arrays var schema = new Schema({ tags: [], name: String }) var Talks = db.model(‘Talk’, schema) Talks.findById(id, function (err, talk) { talk.name = ‘peanut butter’ talk.tags.push(‘nerdcore’) talk.save(callback) // magic! })
  75. 75. MONGOOSE V3General Improvements•Improved update command generation for arrays Object.defineProperty(talk, ‘name’, { get: function () { return this.smth } , set: function (v){ this.smth = v } })
  76. 76. MONGOOSE V3General Improvements•Improved update command generation for arrays Object.defineProperty(talk, ‘name’, { get: function () { return this.smth } , set: function (v){ record(‘name’); this.smth = v } })
  77. 77. MONGOOSE V3General Improvements•Improved update command generation for arrays Object.defineProperty(talk, ‘name’, { get: function () { return this.smth } , set: function (v){ this._set = {} this._set[‘name’] = v this.smth = v } })
  78. 78. MONGOOSE V3General Improvements•Improved update command generation for arrays talk.save() -> var update = {}; update.$set = talk._set; Talks.update({ _id: talk._id }, update, callback)
  79. 79. MONGOOSE V3General Improvements•Improved update command generation for arrays talk.tags.push = function () { record(‘push’, arguments) }
  80. 80. MONGOOSE V3General Improvements•Improved update command generation for arrays talk.tags.push = function () { record(‘push’, arguments) } talk.save() -> var update = {}; update.$pushAll = { tags: args } Talk.update({_id:talk._id}, update, callback)
  81. 81. MONGOOSE V3General Improvements•Improved update command generation for arrays talk.tags.push(‘nerdcore’) talk.tags.remove(‘php’) talk.save(callback) // fail!
  82. 82. MONGOOSE V3General Improvements•Improved update command generation for arrays talk.tags.push(‘nerdcore’) talk.tags.remove(‘php’) talk.save(callback) // success!!
  83. 83. MONGOOSE V3General Improvements•Simplify schema declaration
  84. 84. MONGOOSE V3General Improvements•Simplify schema declaration var personSchema = new Schema({ name: String , val: Number , buffer: Buffer , array: Array , etc: ... })
  85. 85. MONGOOSE V3General Improvements•Simplify schema declaration var personSchema = new Schema({ name: ‘string’ , val: ‘number’ , buffer: ‘buffer’ , array: ‘array’ , etc: ... })
  86. 86. MONGOOSE V3General Improvements•Hardened schema ‘strict’ option var personSchema = new Schema({ name: ‘string’ , val: ‘number’ , buffer: ‘buffer’ , array: ‘array’ , etc: ... }, { strict: true })
  87. 87. MONGOOSE V3General Improvements•Hardened schema ‘strict’ option var personSchema = new Schema({ name: ‘string’ , val: ‘number’ , buffer: ‘buffer’ , array: ‘array’ , etc: ... }, { strict: ‘throw’ })
  88. 88. MONGOOSE V3General Improvements•Hardened schema ‘strict’ option new Person({ notExists: ‘3vil’ })
  89. 89. MONGOOSE V3General Improvements•Hardened schema ‘strict’ option new Person({ notExists: ‘3vil’ }) // throws “Field `notExists` is not in schema”
  90. 90. MONGOOSE V3General Improvements•Simplify schema declaration•Hardened schema ‘strict’ option (throws)•Removed MongooseNumber
  91. 91. MONGOOSE V3General Improvements•Simplify schema declaration•Hardened schema ‘strict’ option (throws)•Removed MongooseNumber•Add multiple validators
  92. 92. MONGOOSE V3General Improvements•Simplify schema declaration•Hardened schema ‘strict’ option (throws)•Removed MongooseNumber•Add multiple validators•toObject,toJSON options
  93. 93. MONGOOSE V3General Improvements•Simplify schema declaration•Hardened schema ‘strict’ option (throws)•Removed MongooseNumber•Add multiple validators•toObject,toJSON options•Specify a model in query.populate()
  94. 94. MONGOOSE V3General Improvements•Add document versioning * soon!
  95. 95. MONGOOSE V3General Improvements•Add document versioning•Recursive populate * soon!
  96. 96. MONGOOSE V3Documentation•mongoosejs.com•plugins.mongoosejs.com•blog.mongoosejs.com
  97. 97. MONGOOSE V3Support•LearnBoost
  98. 98. MONGOOSE V3Support•LearnBoost•10gen *
  99. 99. MONGOOSE V3Support•LearnBoost•10gen * driver too
  100. 100. MONGOOSE V3 ¿Preguntas?@aaronheckmann aaronheckmann@gmail.com@10gen https://github.com/aheckmann@learnboost https://github.com/LearnBoost/mongoose@mongodb
  101. 101. MONGOOSE V3 Thanks!@aaronheckmann aaronheckmann@gmail.com@10gen https://github.com/aheckmann@learnboost https://github.com/LearnBoost/mongoose@mongodb

×