Your SlideShare is downloading. ×
0
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Mongoskin - Guilin
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Mongoskin - Guilin

4,668

Published on

NodeParty-SH-1

NodeParty-SH-1

Published in: Technology, Business
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,668
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
53
Comments
0
Likes
10
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. by 桂林
  • 2. All languages usedBrowser script: javaS c r ip tServer script: javaS c r ip tDatabase script: javaS c r ip t
  • 3. All languages usedBrowser script: javaS c r ip tServer script: javaS c r ip tDatabase script: javaS c r ip t
  • 4. Overview of MongoDBPatterns of mongoskin
  • 5. Why MongoDBAgile and Scalable
  • 6. Overview of MongoDBMySQLposts idcomments id post_idtags id nameposts_tags post_id tag_id
  • 7. Overview of MongoDBDatabase >> DatabaseTable >> CollectionRow >> Document
  • 8. Overview of MongoDBM on g oD B{ _id: ObjectId(), title: node and mongodb, slug: node-and-mongodb, body: ..., published: true, created: new Date(09/01/2011), updated: new Date(09/16/2011), comments: [ { author: bob, email: bob@bob.me, body: ..., created: new Date(09/17/2011) } ], tags: [MongoDB, database]}
  • 9. Overview of MongoDBGet c ollec t ionMongoDB shellposts = db.postsNodeJSvar mongodb = require(mongodb);var db = new Db(test, new Server(host, port, {}),{native_parser:true});db.open(function(err, db){ db.collection(posts, function(err, posts){ // use posts collection here });});
  • 10. Overview of MongoDBI n ser tMongoDB shellvar doc = {title: test posts, ...};posts.insert(doc);NodeJSposts.insert(doc, function(err, reply){ // error or done});
  • 11. Overview of MongoDBQ u er yMongoDB shellposts.find({"comments.author", "bob"})NodeJSposts.find({"comments.author", "bob"}, function(err, cursor){ cursor.toArray(function(err, docs){ // found docs here });});
  • 12. Overview of MongoDBQ u er y op er at or{<field>:{<operator>:<value>}}db.things.find({j:{$in: [2,4,6]}}); $gt, $lt, $gte, $lte $all, $exists $mod, $ne $in, $nin $nor, $or, $and $size, $type, …
  • 13. Overview of MongoDBUp d at eMongoDB shellposts.update({_id: doc.id}, {$inc: {votes: 1}})NodeJSposts.update({_id: doc.id}, {$inc: {votes: 1}}, function(err, count){});
  • 14. Overview of MongoDBM od if ier op er at ion s $set – set a particular value $unset – delete a particular field (v1.3+) $inc – increment a particular value by a certain amount $push – append a value to an array $pushAll – append several values to an array $pull – remove a value(s) from an existing array $pullAll – remove several value(s) from an existing array $bit – bitwise operations
  • 15. Overview of MongoDBR em oveMongoDB shellposts.remove({author: bob})NodeJSposts.remove({author: bob}, function(err, reply){})
  • 16. Overview of MongoDBI n d exMongoDB shellposts.ensureIndex({author:1})posts.ensureIndex({slug: 1 }, {unique: true});db.things.ensureIndex( {firstname: 1, lastname: 1}, {unique: true, background:true});NodeJSposts.ensureIndex({author:1}, function(err, reply){})posts.ensureIndex({slug: 1 }, {unique: true}, function(err, reply){});db.things.ensureIndex( {firstname: 1, lastname: 1}, {unique: true, background:true}, function(err, reply){});
  • 17. Overview of MongoDBGeosp at ial{ loc : [ 50 , 30 ] } //SUGGESTED OPTION{ loc : { x : 50 , y : 30 } }{ loc : { foo : 50 , y : 30 } }{ loc : { lon : 40.739037, lat: 73.992964 } }db.places.ensureIndex( { loc : "2d" } )db.places.find( { loc : { $near : [50,50] , $maxDistance : 5 } } )box = [[40.73083, -73.99756], [40.741404, -73.988135]]db.places.find({"loc" : {"$within" : {"$box" : box}}})center = [50, 50]radius = 10db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})
  • 18. Overview of MongoDBO p t im izat ion Don’t create index for every field Be careful about single-key indexes with low selectivity. Only one index could be used per query. Use compound-key index. db.places.ensureIndex( { location : “2d” , category : 1 } ); db.places.find( { location : { $near : [50,50] }, category : ‘coffee’ } ); Use hint. Use explain. Use the profiler. Pay attention to the read/write ratio of your application.See docs for more information
  • 19. Overview of MongoDBAg g r eg at ion posts.count( {author: bob} ) posts.distinct("author")SQL group select a,b,sum(c) csum from coll where active=1 group by a,bMongoDB group db.coll.group( {key: { a:true, b:true }, cond: { active:1 }, reduce: function(obj, out) { out.csum += obj.c; }, initial: { csum: 0 } });
  • 20. Overview of MongoDBM ap R ed u c emap = function() { for (var i in this.tags) { emit(this.tags[i], 1); }}reduce = function(key, values) { var count = 0; for (var i in values) { count += current[i]; } return count;}// 1.8+ must set out collectiondb.posts.mapReduce(map, reduce, {out: tags_count})
  • 21. Overview of MongoDBM ap R ed u c e> db.tags_count.find(){"_id" : "MongoDB", "value" : 4}{"_id" : "Map/Reduce", "value" : 2}{"_id" : "Recipe", "value" : 7}{"_id" : "Group", "value" : 1}
  • 22. Overview of MongoDBGr id F SUpload imagevar gridStore = new GridStore(db, filename, "w");gridStore.open(function(err, gridStore) { gridStore.write(imageData, function(err, gridStore) { gridStore.close(function(err, result) { console.log(result._id); users.update({_id: userId}, {$set: {avatarId: result._id}}); }); });});HTML<img src="http://asset.url/gridfs/{{ user.avatarId }}" /> Use nginx-gridfs
  • 23. Overview of MongoDBR ep lic at ion and S h ar d in g
  • 24. Nice
  • 25. Nice ,but node
  • 26. Nest ed c allb ac k s, an d n ot D R Y ?var database = new mongo.Db(testdb, new mongo.Server(localhost,27017));database.open(function(err, db) { if(err) return handle(err); db.collection(user, function(err, collection) { if(err) return handle(err); collection.find({}, function(err, cursor) { if(err) return handle(err); cursor.toArray(function(err, users) { if(err) return handle(err); doSomething(users); }); }); });});
  • 27. How t o exp or t c ollec t ionvar database = new mongo.Db(testdb, new mongo.Server(localhost,27017));database.open(function(err, db){ db.collection(posts, function(err, posts) { // cant export here? });});exports.posts = ?
  • 28. How t o sh ar e c ollec t ion ob jec tcontrollers/user.jsvar database = new mongo.Db(testdb, new mongo.Server(localhost,27017));//...database.open(function(err, db){ db.collection(user, function(err, userColl){ userColl.find({}, function(err, cursor){ if(err) return handle(err); cursor.toArray(function(err, users){ res.render(/user.html, {users: users}); }) }); });});
  • 29. How t o sh ar e c ollec t ion ob jec tcontrollers/book.jsvar database = new mongo.Db(testdb, new mongo.Server(localhost,27017));//...database.open(function(err, db){ db.collection(user, function(err, userColl){ userColl.findOne({_id: book.author_id}, function(err, author){ res.render(/book.html, {book: book, author: author}); }); });});
  • 30. Redsign the API
  • 31. How ab ou t t h isconfig.jsexports.db = mongo.db(mongo://localhost:27017/testdb)
  • 32. An d t h iscontrollers/user.jsvar db = require(../config).db;db.collection(user).find({}).toArray(function(err, users){ if(err) return handle(err); res.render(/user.html, {users: users});});
  • 33. An d t h iscontrollers/book.jsvar db = require(../config).db;db.collection(user).findOne({_id, book.author_id}, function(err,author){ if(err) return handle(err); res.render(/book.html, {book: book, author: author});});
  • 34. It’s MongoSkin
  • 35. Patterns of mongoskinvar mongoskin = require(mongoskin);var db = mongoskin.db(mongo://localhost:27017/testdb);db.bind(users);db.bind(books);db.users.find({}).limit(10).sort({name:-1}).toArray(function(err,users){});db.books.update({_id: bookId}, {$inc: {votes: 1}}, function(err,reply){});
  • 36. Patterns of mongoskinPr oxy all m et h od snode-mongoskinvar skindb = mongoskin.db(mongo://localhost:27017/testdb)function callback(err, reply){}skindb.addUser(foo, bar, callback);node-mongodb-nativevar db = new mongodb.Db(testdb, new mongodb.Server(localhost, 27017));function callback(err, reply){}db.open(function(err, db){ if(err) return callback(err); db.addUser(foo, bar, callback);});
  • 37. Patterns of mongoskinSkinClass hold all parameters to open itvar SkinDb = exports.SkinDb = function(db, username, password) { this.db = db; this.username = username; this.password = password; this.state = STATE_CLOSE; this.emitter = new events.EventEmitter(); this._collections = {};};
  • 38. Patterns of mongoskinProxy all methods inside openfor (var name in Db.prototype) { SkinDb.prototype[name] = function() { var args = Array.prototype.slice.call(arguments); this.open(function(err, db) { if (err) { return args[args.length - 1](err);//callback(err) } else { return Db.prototype[name].apply(db, args); } }); };}
  • 39. Patterns of mongoskinOpen only on c e (pseudo-code)SkinDb.prototype.open = function(fn) { switch (this.state) { case STATE_OPEN: return fn(null, this.db); case STATE_OPENNING: // if call open method multi times before opened return this.emitter.addListener(open, fn); case STATE_CLOSE: this.state = STATE_OPENNING; var that = this; this.db.open(function(err, db){ that.db = db; fn(err, db); that.state = STATE_OPEN; that.emitter.emit(open, err, db); }); }
  • 40. } }; Patterns of mongoskinR et u r n s S k in Collec t ion SkinDb.prototype.collection = function(name) { var collection = this._collections[name]; if (!collection) { this._collections[name] = collection = new SkinCollection(this, name); } return collection; };
  • 41. Patterns of mongoskinS k in Collec t ion var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) { this.skinDb = skinDb; this.collectionName = collectionName; this.collection; this.state = STATE_CLOSE; this.internalHint; var that = this; this.__defineGetter__(hint, function() { return this.internalHint; }); this.__defineSetter__(hint, function(value) { this.internalHint = value; this.open(function(err, collection) { collection.hint = value; that.internalHint = collection.hint; }); }); this.emitter = new events.EventEmitter(); }
  • 42. } Patterns of mongoskinPr oxy all m et h od s in sid e S k in Collec t ion . op enfor (var name in Collection.prototype) { SkinCollection.prototype[name] = function() { var args = Array.prototype.slice.call(arguments); this.open(function(err, collection) { if (err) { args[args.length - 1](err);// callback(err) } else { Collection.prototype[name].apply(collection, args); } }); };}
  • 43. Patterns of mongoskinS k in Collec t ion . op en SkinCollection.prototype.open = function(callback) { //... var that = this; this.skinDb.open(function(err, db){ db.collection(that.collectionName, function(err, collection){ this.nativeCollection = collection; callback(err, collection); } }); }
  • 44. Patterns of mongoskinR et u r n s S k in Cu r sor if n o c allb ac k SkinCollection.prototype.find = function() { var args = Array.prototype.slice.call(arguments); if (args.length > 0 && typeof(args[args.length - 1]) === function) { this._find.apply(this, args); }else { return new SkinCursor(null, this, args); } };An d so d o w it h S k in Cu r sor
  • 45. Patterns of mongoskinPat t er n s of m on g osk in SkinClass contains all parameters to get NativeObject Proxy all method inside callback of op en ( ) Make open() method cache result Return SkinClass object to chain execution
  • 46. ReferenceMongoDB The Definitive GuideMongoDB Docs http://www.mongodb.org/display/DOCS/HomeMongoDB Cookbook http://cookbook.mongodb.org/Node mongodb https://github.com/christkv/node-mongodb-nativeMongoskin https://github.com/guileen/node-mongoskin
  • 47. Thank you 桂糊涂@weibo guileen@gmail.com

×