Lets learn the philosophy NOSQL takes (from a developer's standpoint), the changes you'll (not) have to take, discuss mongo, and see some practical examples!
These are my first revision of this talk and will be making some organizational improvements late.
2. Who Am I?
• Daniel Cousineau
• Sr. Software Applications Developer at
Texas A&M University
• dcousineau@gmail.com
• Twitter: @dcousineau
• dcousineau.com
23. Why MongoDB?
• Because it’s what I need
• Because I understand it
• Because I’ve used it
• Because it’s easy
• Because it has superior driver support
• Because I said so
24. Support?
• Operating Systems • Official Drivers
• OSX 32/64bit • C, C++, Java,
JavaScript, Perl, PHP,
• Linux 32/64bit Python, Ruby
• Windows 32/64bit • Community Drivers
• Solaris i86pc • REST, C#, Clojure, ColdFusion,
Delphi, Erlang, Factor, Fantom, F#,
• Solaris 64 Go, Groovy, Haskell, Lua, Obj-C,
PowerShell, Scala, Scheme, Smalltalk
25. What is MongoDB?
• A document-based storage system
• Databases contain collections, collections
contain documents
• Documents are arbitrary BSON (extension
of JSON) objects
• No schema is enforced
26. What is MongoDB?
• Drivers expose MongoDB query API to
languages in a form familiar and native
• Drivers usually handle serialization
• You always work in native system
objects, BSON is really only used
internally
29. Simple Queries
$ ./mongodb-osx-x86_64-1.6.0/bin/mongo
MongoDB shell version: 1.6.0
connecting to: test
> db.foo.find()
Get All Records
> db.foo.find( {a: 1} )
Get All Records Where Property ‘a’ Is 1
> db.foo.find( {a: 1}, {_id: 1} )
Get The ‘_id’ Property Of All Records Where ‘a’ Is 1
> db.foo.find().limit( 1 )
Get 1 Record
> db.foo.find().sort( {a: -1} )
Get All Records Sorted By Property ‘a’ In Reverse
>
31. So I should chose
MongoDB over MySQL?
• Bad Question!
• 90% of the time you’ll probably implement
a hybrid system.
32. When should I use
MongoDB?
• When an ORM is necessary
• It’s in the name, Object-Relational
Mapper
• When you use a metric ton of 1:n and n:m
tables to generate 1 object
• And you rarely if ever use them for
reporting
33. MongoDB performance
is better?
• Too simple of a question
• Performance comparable, MySQL usually
wins sheer query speed
• Sterilized Lab Test
• MongoDB usually wins due to fewer
queries required and no object reassembly
• The Real World
34. Can MongoDB enforce
a schema?
• You can add indexes on arbitrary
keypatterns
• Otherwise, why?
• Application is responsible for correctness
and error handling, no need to duplicate
35. Can I trust eventual
consistency?
• No, but you shouldn’t trust ACID either
• Build your application to be flexible and to
handle consistency issues
• Stale data is a fact of life
36. Can MongoDB Handle
Large Deployments?
1.2 TB over 5 billion records
600+ million documents
Migrating ENTIRE app from Postgres
http://www.mongodb.org/display/DOCS/Production+Deployments
37. Can MongoDB Handle
Large Deployments?
• huMONGOousDB
• 32-bit only supports ~2.5GB
• Memory-mapped files
• Individual documents limited to 4MB
41. Very Simple
• Daemon does insertion in the background
• Front end just does simple grabs
• Grab 1, Grab Many, etc.
42. Data Model
• System has Stocks
• Each Stock has Daily (once per day) and
IntraDaily (every 15 minutes) data
• Limited to trading hours
• Each set of data (daily or intradaily) has 4
Graphs
• Each graph has upwards of 6 Lines
• Each line has between 300 to 800 Points
43. Data Model
• With each data point representing 1
minute, each 15-minute IntraDay graph will
have about 785 overlapping points with the
preceding graph
• Why not consolidate into a single running
table, and just SELECT ... LIMIT 800
points from X timestamp?
44. Data Model
• The Algorithm will cause past points to
change
• But each graph should be preserved so one
can see the historical evolution of a given
curve
45. Data Model
• Now imagine implementing these
requirements in a traditional RDBMS
56. Not Too Terrible
• Keep track of Student Cases
• A case keeps track of demographics,
diagnoses, disabilities, notes, schedule, etc.
• Also tracks Exams
• Schedule multiple exams per course
• Finally, students can log into a portal,
counselors can log in
• Basic user management
71. Number Registered On
Or Before 2010/06/20?
> db.students
.find( {$where: "new Date(this.registrationdate) <= new Date('2010/06/20')"} )
.count()
136
>
75. How Many Students In
A Course?
> db.students
.find({'schedules.courses': {$in: [
new DBRef('courses', new ObjectId('4c60dae48ead0e143e000000'))
]}})
.count()
25
76. No Time To Cover...
• Map-Reduce
• MongoDB has it, and it is extremely
powerful
• GridFS
• Store files/blobs
• Sharding/Replica Pairs/Master-Slave