Mais conteúdo relacionado Semelhante a Schema design with MongoDB (Dwight Merriman) (20) Schema design with MongoDB (Dwight Merriman)4. Choose a schema that makes queries easy makes queries fast facilitates atomicity facilitates sharding 5. Key question: embed vs. link “Contains relationship” : embed Embed = “pre-joined” Links: client/server turnarounds On a close call, embed. Use rich documents. Note the 4MB object size limit Arbitrary limit but pushes one towards good designs 12. { _id : … } Should be: Unique Invariant Ideally, not reused (delete; insert) ObjectID type often best for a sharded collection 14. Single “Table” Inheritance Works Well > t.find() { type:’irregular-shape’, area:99 } { type:’circle’, area:3.14, radius:1 } { type:’square’, area:4, d:2 } { type:’rect’, area:8, x:2, y:4 } > t.find( { radius : { $gt : 2.0 } } ) > t.ensureIndex( { radius : 1 } ) // fine 23. (3) Array of Ancestors > t = db.mytree; > t.find() { "_id" : "a" } { "_id" : "b", "ancestors" : [ "a" ], "parent" : "a" } { "_id" : "c", "ancestors" : [ "a", "b" ], "parent" : "b" } { "_id" : "d", "ancestors" : [ "a", "b" ], "parent" : "b" } { "_id" : "e", "ancestors" : [ "a" ], "parent" : "a" } { "_id" : "f", "ancestors" : [ "a", "e" ], "parent" : "e" } { "_id" : "g", "ancestors" : [ "a", "b", "d" ], "parent" : "d" } > t.ensureIndex( { ancestors : 1 } ) > // find all descendents of b: > t.find( { ancestors : 'b' }) { "_id" : "c", "ancestors" : [ "a", "b" ], "parent" : "b" } { "_id" : "d", "ancestors" : [ "a", "b" ], "parent" : "b" } { "_id" : "g", "ancestors" : [ "a", "b", "d" ], "parent" : "d" } > // get all ancestors of f: > anc = db.mytree.findOne({_id:'f'}).ancestors [ "a", "e" ] > db.mytree.find( { _id : { $in : anc } } ) { "_id" : "a" } { "_id" : "e", "ancestors" : [ "a" ], "parent" : "a" } 25. Compare and Swap > t=db.inventory > s = t.findOne( {sku:'abc'} ) > --s.qty; > t.update({_id:s._id, qty:qty_old}, s); > db.getLastError() {"err" : , "updatedExisting" : true , "n" : 1 , "ok" : 1} // it worked 26. Compare and Swap > t=db.inventory > s = t.findOne( {sku:'abc'} ) > --s.qty; > t.update({_id:s._id, qty:qty_old}, s); > db.getLastError() {"err" : , "updatedExisting" : true , "n" : 1 , "ok" : 1} // it worked Oops? 27. Compare and Swap - Better > t=db.inventory > s = t.findOne( {sku:'abc'} ) > obj_old = Object.extend({}, s); > --s.qty; > // t.update({_id:s._id, qty:qty_old}, s); > t.update( obj_old , s); > print( db.getLastError().ok ? “worked” : “try again” ); 28. Compare and Swap – versions update( { _id : myid, ver : last_ver }, { $set : { x : “abc”, y : 99 }, $inc : { ver : 1 } } ) 30. Compare and Swap > t=db.inventory > s = t.findOne( {sku:'abc'} ) > --s.qty; > t.update({_id:s._id, qty:qty_old}, s); > db.getLastError() {"err" : , "updatedExisting" : true , "n" : 1 , "ok" : 1} // it worked 31. Compare and Swap > t=db.inventory > s = t.findOne( {sku:'abc'} ) > --s.qty; > t.update({_id:s._id, qty:qty_old}, s); > db.getLastError() {"err" : , "updatedExisting" : true , "n" : 1 , "ok" : 1} // it worked 32. Sharding and Schemas Shard key selection Restrictions on unique indexes Consider using may collections when that is natural 10 SN 34. Questions? Get involved with the MongoDB project! Coding, drivers, frameworks, documentation, translation, consulting, evangelism, suggestions, vote on jira…spread the word.