50
Schema Design J. Randall Hunt Developer and Evangelist at MongoDB

Schema Design in MongoDB - TriMug Meetup North Carolina

Embed Size (px)

DESCRIPTION

The Schema Design talk given at the TriMug meetup in Durham, NC

Citation preview

Page 1: Schema Design in MongoDB - TriMug Meetup North Carolina

Schema DesignJ. Randall Hunt Developer and Evangelist at MongoDB

Page 2: Schema Design in MongoDB - TriMug Meetup North Carolina

Who am I?

• J. Randall Hunt

• @jrhunt

• github.com/ranman

[email protected]

Page 3: Schema Design in MongoDB - TriMug Meetup North Carolina

Why are you here?

• To learn about MongoDB

• To engage in the MongoDB community

• To get free stuff

Page 4: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 5: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 6: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 7: Schema Design in MongoDB - TriMug Meetup North Carolina

Levels of Abstraction!

Page 8: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 9: Schema Design in MongoDB - TriMug Meetup North Carolina

ORMs To Save The Day!

Page 10: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 11: Schema Design in MongoDB - TriMug Meetup North Carolina

Why change something that's been around for 40

years?

Page 12: Schema Design in MongoDB - TriMug Meetup North Carolina

10TB

Data Human Kind Has Produced Until 1991

Page 13: Schema Design in MongoDB - TriMug Meetup North Carolina

Data Mankind Produces Every Day Since 2001

10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB

10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB

10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB

10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB

10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB 10TB

Page 14: Schema Design in MongoDB - TriMug Meetup North Carolina

NOSQL/NOREL

Page 15: Schema Design in MongoDB - TriMug Meetup North Carolina

Relational Schema Design Focus on data storage

Page 16: Schema Design in MongoDB - TriMug Meetup North Carolina

Document Schema Design Focus on data use

Page 17: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 18: Schema Design in MongoDB - TriMug Meetup North Carolina

Why MongoDB?• Focus on commodity hardware, not insane machines

• Document Store

• Dynamic Schema

• Sensible Defaults

• Modern Scaling Infrastructure

Page 19: Schema Design in MongoDB - TriMug Meetup North Carolina

How People Use MongoDB• Analytics

• Risk Management

• Caching Layer

• Recommendation Engines

• GIS

Page 20: Schema Design in MongoDB - TriMug Meetup North Carolina

Nitty Gritty

Page 21: Schema Design in MongoDB - TriMug Meetup North Carolina

RDBMS MongoDB Database � Database Table � Collection Row � Document Index � Index Join � Embedded Document Foreign Key � Reference

Page 22: Schema Design in MongoDB - TriMug Meetup North Carolina

Documents?

Page 23: Schema Design in MongoDB - TriMug Meetup North Carolina

{ "hello": "world" }

Page 24: Schema Design in MongoDB - TriMug Meetup North Carolina

{ "_id": ObjectId("51638f8332e9bc556fe86de7"), "dstats": [ { "+": "5", "-": "0", "f": "gitstreamer.py" }, { "+": "3", "-": "3", "f": "post-commit.py" } ], "author": "ranman", "ts": ISODate("2013-04-08T19:48:11-0400"), "project": "gitstreamer", "msg": "turning this into a webapp" } !

Page 25: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 26: Schema Design in MongoDB - TriMug Meetup North Carolina

CRUD

Page 27: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 28: Schema Design in MongoDB - TriMug Meetup North Carolina

test> db.test.find() Fetched 0 record(s) in 1ms -- Index[none]

Page 29: Schema Design in MongoDB - TriMug Meetup North Carolina

test> db.test.find() Fetched 0 record(s) in 1ms -- Index[none]test> db.test.insert({'hello': 'world'}) Inserted 1 record(s) in 1ms Insert WriteResult({ "ok": 1, "n": 1 })

Page 30: Schema Design in MongoDB - TriMug Meetup North Carolina

test> db.test.find() Fetched 0 record(s) in 1ms -- Index[none]test> db.test.insert({'hello': 'world'}) Inserted 1 record(s) in 1ms Insert WriteResult({ "ok": 1, "n": 1 }) test> db.test.find({'hello': 'world'}) { "_id": ObjectId("52d61af21486ef9e06d6d41a"), "hello": "world" } Fetched 1 record(s) in 0ms -- Index[none]

Page 31: Schema Design in MongoDB - TriMug Meetup North Carolina

test> db.test.update({'hello': 'world'}, {$set: {'hello': 'welt'}}) Updated 1 existing record(s) in 0ms Update WriteResult({ "ok": 1, "n": 1 })

test> db.test.find() Fetched 0 record(s) in 1ms -- Index[none]test> db.test.insert({'hello': 'world'}) Inserted 1 record(s) in 1ms Insert WriteResult({ "ok": 1, "n": 1 }) test> db.test.find({'hello': 'world'}) { "_id": ObjectId("52d61af21486ef9e06d6d41a"), "hello": "world" } Fetched 1 record(s) in 0ms -- Index[none]

Page 32: Schema Design in MongoDB - TriMug Meetup North Carolina

Lots of Operators!

Page 33: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 34: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 35: Schema Design in MongoDB - TriMug Meetup North Carolina
Page 36: Schema Design in MongoDB - TriMug Meetup North Carolina

Enough already I know what MongoDB is! Teach me

schema design!

Page 37: Schema Design in MongoDB - TriMug Meetup North Carolina

Library Management

• Patrons

• Books

• Authors

• Publishers

Page 38: Schema Design in MongoDB - TriMug Meetup North Carolina

One To One Relations

Page 39: Schema Design in MongoDB - TriMug Meetup North Carolina

patron = { _id: ObjectId("52d7173817d8bbd9564613cd"), name: 'Joe Schmoe' } !

address = { patron_id: ObjectId("52d7173817d8bbd9564613cd"), street: "100 Five Bridge Rd", city: "Clinton", state: "NC", zip: 28723 } >patron = db.patron.find({'name': 'Joe Schmoe'})[0] >db.address.find('patron_id': patron._id)

Page 40: Schema Design in MongoDB - TriMug Meetup North Carolina

patron = { _id: ObjectId("52d7173817d8bbd9564613cd"), name: 'Joe Schmoe', address: { street: "100 Five Bridge Rd", city: "Clinton", state: "NC", zip: 28723 } } >db.patrons.findOne({'name': /Joe Schmoe/})

Page 41: Schema Design in MongoDB - TriMug Meetup North Carolina

One To Many Relations

Page 42: Schema Design in MongoDB - TriMug Meetup North Carolina

book = { _id: ObjectID(...), title: "MongoDB", authors: ['Kristina Chodorow', 'Mike Dirolf'], published_date: ISODate('2010-09-24'), pages: 216, language: 'English', publisher: { name: "O'Reilly Media", founded: "1980", location: "CA" } }

Page 43: Schema Design in MongoDB - TriMug Meetup North Carolina

4 ways of modeling one-to-many (there are more)

• Embed the publisher

• Use publisher as the "foreign key"

• Use book as the "foreign key"

• Hybrid

Page 44: Schema Design in MongoDB - TriMug Meetup North Carolina

publisher = { _id: "oreilly", name: "O'Reilly Media", founded: "1980", location: "CA" } book = { _id: ObjectID(...), title: "MongoDB", authors: ['Kristina Chodorow', 'Mike Dirolf'], published_date: ISODate('2010-09-24'), pages: 216, language: 'English', publisher_id: 'oreilly' }

Page 45: Schema Design in MongoDB - TriMug Meetup North Carolina

publisher = { name: "OReilly Media", founded: "1980", location: "CA" books: [ ObjectId(...), ... ] } book = { _id: ObjectID(...), title: "MongoDB", authors: ['Kristina Chodorow', 'Mike Dirolf'], published_date: ISODate('2010-09-24'), pages: 216, language: "English" }

Page 46: Schema Design in MongoDB - TriMug Meetup North Carolina

Hybrid Models

• We store the foreign key

• and the info relevant to the relation

Page 47: Schema Design in MongoDB - TriMug Meetup North Carolina

patron = { _id: ObjectId("52d7173817d8bbd9564613cd"), name: "Joe Bookreader", address: {...}, join_date: ISODate("2011-10-15"), books: [ {_id: ObjectID(...), title: "MongoDB", author: "Kristina C.", ...}, {_id: ObjectId(...), title: "Postgres", author: "Randall H.", ...} ] }

Page 48: Schema Design in MongoDB - TriMug Meetup North Carolina

Where do you put the foreign key• Array of books inside of publisher

• Makes sense when many means a handful of items

• Useful when items have bounds on potential growth

• Reference to a single publisher on each book

• Useful when items have unbounded growth

Page 49: Schema Design in MongoDB - TriMug Meetup North Carolina

Other Things To Model

• Trees

• Queues

• Many-To-Many relationships

Page 50: Schema Design in MongoDB - TriMug Meetup North Carolina

Thanks! @jrhunt