Your SlideShare is downloading. ×
0
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
Schema design with MongoDB (Dwight Merriman)
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

Schema design with MongoDB (Dwight Merriman)

12,686

Published on

Published in: Technology
1 Comment
35 Likes
Statistics
Notes
No Downloads
Views
Total Views
12,686
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
350
Comments
1
Likes
35
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. Schema Design with MongoDB<br />Dwight Merriman<br />CEO<br />10gen<br />
  • 2. What is document-oriented?<br />JSON objects<br />Not relational<br />Not OODB<br />Database schema != program “schema”<br />
  • 3. Terms<br /><ul><li>Row -&gt; JSON document</li></ul>Tables -&gt; collections<br />Indexes -&gt; index<br />Join -&gt; embedding and linking<br />
  • 4. Choose a schema that<br /> makes queries easy<br /> makes queries fast<br /> facilitates atomicity<br /> facilitates sharding<br />
  • 5. Key question: embed vs. link<br />“Contains relationship” : embed<br />Embed = “pre-joined”<br />Links: client/server turnarounds<br />On a close call, embed. Use rich documents.<br />Note the 4MB object size limit<br />Arbitrary limit but pushes one towards good designs<br />
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. Map-Reduce<br />
  • 12. { _id : … }<br />Should be:<br />Unique <br />Invariant <br />Ideally, not reused (delete; insert)<br />ObjectID type often best for a sharded collection<br />
  • 13. Trees<br />mongodb.org/display/DOCS/Trees+in+MongoDB<br />
  • 14. Single “Table” Inheritance Works Well<br />&gt; t.find()<br />{ type:’irregular-shape’, area:99 }<br />{ type:’circle’, area:3.14, radius:1 }<br />{ type:’square’, area:4, d:2 }<br />{ type:’rect’, area:8, x:2, y:4 }<br />&gt; t.find( { radius : { $gt : 2.0 } } )<br />&gt; t.ensureIndex( { radius : 1 } ) // fine<br />
  • 15. (1) Full tree in one document<br />{ <br /> comments: [ <br /> {by: &quot;mathias&quot;, text: &quot;...&quot;, <br /> replies: []} <br /> {by: &quot;eliot&quot;, text: &quot;...&quot;, <br /> replies: [ <br /> {by: &quot;mike&quot;, text: &quot;...&quot;, replies: []}<br /> ]}<br /> ] <br />}<br /><ul><li>Pros:
  • 16. Single document to fetch per page
  • 17. One location on disk for whole tree
  • 18. You can see full structure easily
  • 19. Cons:
  • 20. Hard to search
  • 21. Hard to get back partial results
  • 22. 4MB limit</li></li></ul><li>(2) Parent Links<br />&gt; t = db.tree1;<br />&gt; t.find()<br />{ &quot;_id&quot; : 1 }<br />{ &quot;_id&quot; : 2, &quot;parent&quot; : 1 }<br />{ &quot;_id&quot; : 3, &quot;parent&quot; : 1 }<br />{ &quot;_id&quot; : 4, &quot;parent&quot; : 2 }<br />{ &quot;_id&quot; : 5, &quot;parent&quot; : 4 }<br />{ &quot;_id&quot; : 6, &quot;parent&quot; : 4 }<br />&gt; // find children of node 4<br />&gt; t.ensureIndex({parent:1})<br />&gt; t.find( {parent : 4 } )<br />{ &quot;_id&quot; : 5, &quot;parent&quot; : 4 }<br />{ &quot;_id&quot; : 6, &quot;parent&quot; : 4 }<br />// hard to get all descendants<br />
  • 23. (3) Array of Ancestors<br />&gt; t = db.mytree;<br />&gt; t.find()<br />{ &quot;_id&quot; : &quot;a&quot; }<br />{ &quot;_id&quot; : &quot;b&quot;, &quot;ancestors&quot; : [ &quot;a&quot; ], &quot;parent&quot; : &quot;a&quot; }<br />{ &quot;_id&quot; : &quot;c&quot;, &quot;ancestors&quot; : [ &quot;a&quot;, &quot;b&quot; ], &quot;parent&quot; : &quot;b&quot; }<br />{ &quot;_id&quot; : &quot;d&quot;, &quot;ancestors&quot; : [ &quot;a&quot;, &quot;b&quot; ], &quot;parent&quot; : &quot;b&quot; }<br />{ &quot;_id&quot; : &quot;e&quot;, &quot;ancestors&quot; : [ &quot;a&quot; ], &quot;parent&quot; : &quot;a&quot; }<br />{ &quot;_id&quot; : &quot;f&quot;, &quot;ancestors&quot; : [ &quot;a&quot;, &quot;e&quot; ], &quot;parent&quot; : &quot;e&quot; }<br />{ &quot;_id&quot; : &quot;g&quot;, &quot;ancestors&quot; : [ &quot;a&quot;, &quot;b&quot;, &quot;d&quot; ], &quot;parent&quot; : &quot;d&quot; }<br />&gt; t.ensureIndex( { ancestors : 1 } )<br />&gt; // find all descendents of b:<br />&gt; t.find( { ancestors : &apos;b&apos; })<br />{ &quot;_id&quot; : &quot;c&quot;, &quot;ancestors&quot; : [ &quot;a&quot;, &quot;b&quot; ], &quot;parent&quot; : &quot;b&quot; }<br />{ &quot;_id&quot; : &quot;d&quot;, &quot;ancestors&quot; : [ &quot;a&quot;, &quot;b&quot; ], &quot;parent&quot; : &quot;b&quot; }<br />{ &quot;_id&quot; : &quot;g&quot;, &quot;ancestors&quot; : [ &quot;a&quot;, &quot;b&quot;, &quot;d&quot; ], &quot;parent&quot; : &quot;d&quot; }<br />&gt; // get all ancestors of f:<br />&gt; anc = db.mytree.findOne({_id:&apos;f&apos;}).ancestors<br />[ &quot;a&quot;, &quot;e&quot; ]<br />&gt; db.mytree.find( { _id : { $in : anc } } )<br />{ &quot;_id&quot; : &quot;a&quot; }<br />{ &quot;_id&quot; : &quot;e&quot;, &quot;ancestors&quot; : [ &quot;a&quot; ], &quot;parent&quot; : &quot;a&quot; }<br />
  • 24. Atomicity<br />Atomicity at the document level<br />$operators<br />Compare and swap<br />
  • 25. Compare and Swap<br />&gt; t=db.inventory<br />&gt; s = t.findOne( {sku:&apos;abc&apos;} )<br />&gt; --s.qty;<br />&gt; t.update({_id:s._id, qty:qty_old}, s);<br />&gt; db.getLastError()<br />{&quot;err&quot; : , &quot;updatedExisting&quot; : true , &quot;n&quot; : 1 , &quot;ok&quot; : 1} // it worked <br />
  • 26. Compare and Swap<br />&gt; t=db.inventory<br />&gt; s = t.findOne( {sku:&apos;abc&apos;} )<br />&gt; --s.qty;<br />&gt; t.update({_id:s._id, qty:qty_old}, s);<br />&gt; db.getLastError()<br />{&quot;err&quot; : , &quot;updatedExisting&quot; : true , &quot;n&quot; : 1 , &quot;ok&quot; : 1} // it worked <br />Oops?<br />
  • 27. Compare and Swap - Better<br />&gt; t=db.inventory<br />&gt; s = t.findOne( {sku:&apos;abc&apos;} )<br />&gt; obj_old = Object.extend({}, s);<br />&gt; --s.qty;<br />&gt; // t.update({_id:s._id, qty:qty_old}, s);<br />&gt; t.update( obj_old , s);<br />&gt; print( db.getLastError().ok ? “worked” : “try again” );<br />
  • 28. Compare and Swap – versions<br />update( { _id : myid, ver : last_ver },<br /> { $set : { x : “abc”, y : 99 },<br /> $inc : { ver : 1 }<br /> } )<br />
  • 29. Of course, don’t both doing CASwhen you don’t have to<br />&gt; t.update( { sku : “abc”,<br /> qty : {$gt:0} }, <br /> { $inc : { qty : -1 } } <br /> )<br /><ul><li>db.getLastError()</li></ul>{ &quot;updatedExisting&quot; : true , &quot;n&quot; : 1 , &quot;ok&quot; : 1 }<br />{ &quot;updatedExisting&quot; : false , &quot;n&quot; : 0 , &quot;ok&quot; : 1 }<br />
  • 30. Compare and Swap<br />&gt; t=db.inventory<br />&gt; s = t.findOne( {sku:&apos;abc&apos;} )<br />&gt; --s.qty;<br />&gt; t.update({_id:s._id, qty:qty_old}, s);<br />&gt; db.getLastError()<br />{&quot;err&quot; : , &quot;updatedExisting&quot; : true , &quot;n&quot; : 1 , &quot;ok&quot; : 1} // it worked <br />
  • 31. Compare and Swap<br />&gt; t=db.inventory<br />&gt; s = t.findOne( {sku:&apos;abc&apos;} )<br />&gt; --s.qty;<br />&gt; t.update({_id:s._id, qty:qty_old}, s);<br />&gt; db.getLastError()<br />{&quot;err&quot; : , &quot;updatedExisting&quot; : true , &quot;n&quot; : 1 , &quot;ok&quot; : 1} // it worked <br />
  • 32. Sharding and Schemas<br />Shard key selection<br />Restrictions on unique indexes<br />Consider using may collections when that is natural<br /> 10 SN<br />
  • 33. Other Considerations<br />Cappedcollections<br />
  • 34. Questions?<br />Get involved with the MongoDB project!<br />Coding, drivers, frameworks, documentation, translation, consulting, evangelism, suggestions, vote on jira…spread the word.<br />

×