Download presentation
Presentation is loading. Please wait.
Published byLoren Creek Modified over 9 years ago
1
{ NAME: “Marc Esher”, TWEETER: “@marcesher”, WORK_FOR: “Booz | Allen | Hamilton”, CONTRIBUTE_TO: [ “MXUnit”, “CFMongoDB” ], URL: “http://tinyurl.com/MongoRIAUnleashed”, DATE: new Date(’11/12/2010’) } And I’m going to show you: why MongoDB is Awesome
2
Our Journey Pop Quiz Why MongoDB For CF-ers? The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
3
What datatype is this?
4
And This?
7
And the first picture is different from that last one how? VS.
8
Mindshift From Queries To Arrays of Structures
9
MongoDB is… A Schema-less, Document-Oriented datastore Documents are roughly equivalent to CF structs: Keys, values Values can be other documents (i.e. embedded) Documents are searchable
10
MongoDB’s Data Model A Database has “Collections” Collections have “Documents” Documents have “Fields” Fields are key = value pairs A Collection does not enforce the structure of its documents* *i.e. Schemaless
11
Our Journey Pop Quiz Why MongoDB For CF-ers? The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
12
Why MongoDB for CF-ers? “It’s so easy, even a ColdFusion ‘programmer’ can use it” -- anonymous PHP scriptmonkey
13
This time, with feeling Familiar Datatypes MongoDB Document == CF Struct MongoDB Collection == Arrays of Structs
14
And then…. Ad hoc queries “language” for querying these collections Secondary indexes to support these queries
15
MongoDB and ColdFusion 10gen (MongoDB makers) builds the Java drivers Drop the jars into your cfusion lib Or use JavaLoader Then use Mongo like any other object CFMongoDB can make all of this a breeze
16
Our Journey Pop Quiz Why MongoDB For CF-ers? Surprise Pop Quiz! The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
17
What datatype is this? “2”
18
What datatype is this? “true”
19
What datatype is this? false
20
What datatype is this? {married=“true”, age=35}
21
It ain’t all puppies and unicorns 1 != “1” true != “true” 2.546 != “2.546” {age=35} != {AGE=35} {lastname= 1,firstname= 1 } != {lastname: 1,firstname: 1 }* Lucky for you, CFMongoDB solves *most* of these problems * HUH? When we get to.sort(), you’ll see
22
Get your mind right Think in JavaScript When you’re creating CFML structures, pretend you’re creating JavaScript objects Case Matters Datatype matters (unless you use CFMongoDB)
23
Our Journey Pop Quiz Why MongoDB For CF-ers? The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
24
The “Feel” of MongoDB Relax… we’ll do more bullet points later
25
Install and Run MongoDB Download from mongodb.org Unzip Create data directory >mkdir c:\data\db Run MongoDB (mongod): >cd c:\mongodb-1.6.3\bin >mongod Run Mongo shell (mongo): >mongo Yes, that’s it
26
The Mongo Shell >mongo >help() >show dbs >use >show collections >db.collectionName.findOne() >db.collectionName.find() >db.help() >db.collectionName.help()
27
Insert See that new “_id” field? We’ll discuss that later
28
Update
29
Remove
30
Finding by nested fields
33
Finding with Conditionals
35
What happens if I search on a field a Document doesn’t have?
38
Can I Find Documents without XXX?
39
Sorting? Limiting? Skipping? Have you ever had to “Page” in a web app?
40
Sorting? 1 = true, 1 = asc, 1 = include -1 = false, -1 = desc, -1 = exclude (depends on context)
41
Sorting? Limiting? sort(), skip(), limit()
42
Sorting + Limiting + Skipping = Paging sort(), skip(), limit()
43
count() and distinct()
47
And so much more! http://www.mongodb.org/display/DOCS/Advanced+Queries
48
Schema Design Guideline Embed when you can: { NAME = “Marc”, KIDS = [{NAME=“Lexie”}, {NAME=“Sidney”}] } Relate when you must: { NAME = “Marc”, KIDS = [ ObjectId(4cd0bcc754503766b7dcfd6a), ObjectId(4cd0bcc754503766b7dcfd7a) ] } http://www.mongodb.org/display/DOCS/Schema+Design (and watch the presentations, too)
49
Our Journey Pop Quiz Why MongoDB For CF-ers? The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
50
Why MongoDB: The Official Word MongoDB is Big and Fast (just like every other NoSQL Datastore)
51
Why MongoDB: Performance No Joins + No multi-row transactions = Fast Reads = Fast Writes (b/c you write to fewer tables, no trans. log) Async writes = you don’t wait for inserts to complete (optional, though) Secondary Indexes = Index on embedded document fields for superfast ad-hoc queries Indexes live in RAM
52
Why MongoDB: Scalability / Availability No Joins + No multi-row transactions = Horizontally Scalable = Built for Distribution of data and computation Sharding for distributed data MapReduce for distributed computation Built-in Replication Easy to configure Reads can be distributed across slaves Automatic failover when using Replica Sets Shard for scalability, replicate for availability
53
Why MongoDB: R.A.D. Documents (think: objects) with embedded documents feel more natural compared with DB / ORM Ad-hoc queries don’t require “view” creation or other config… code-n-go No penalty for schema evolution (true for most NoSQL stores) Atomic Modifiers for safe in-place updates (no locking code around single documents) Built-in functions for Mongo-as-a-queue (I LOVE these!)
54
Why MongoDB: Friendly License You can use MongoDB in your corporate app You do not have to open source your corporate app From the docs: “ To reiterate, you only need to provide the source for the MongoDB server and not your application” http://www.mongodb.org/display/DOCS/Licensing
55
Why MongoDB: Marc’s Favorite Things Structs, how do I love thee? Inserts/Updates/Upserts are soooo easy Most days, I want to kneecap ORM… not so with Mongo Frequently “embedded” more natural than “relational” Queries, queries, queries. Indexes, Indexes, Indexes Functionality built for “paging”: sort, limit, skip, count, size Horizontal Scaling makes sense to me Docs are Fantastic Examples, examples, examples One word: findAndModify(). U-Ni-Corns MongoDB has brought Joy into my development
56
Our Journey Pop Quiz Why MongoDB For CF-ers? The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
57
Why Not MongoDB? Vs. Relational DBs No Joins You’ll embed instead You’ll join in your application code when necessary No Transactions Might want to rethink using MongoDB for bank account transfers Though if you consider a “trade” as a single document representing both sides of the transaction… remember, mindshift You WILL miss SQL! Though you’ll get used to MapReduce (which is just JS functions!) Perhaps not a great fit for Reporting-heavy apps Though die-hards would debate this It simply may not be a best fit for your app Relational databases are not going anywhere CoSQL?
58
Why Not MongoDB? Vs. Itself Still kinda new Though used in production at major sites Evolving rapidly This is a good thing, though you’ll want to keep up with new releases Not meant for 32Bit You’ll have a 2GB limit per mongod process if you do No single-server durability If you’re not running replication, and it crashes, you will lose data (they’re working on this) Just replicate, period
59
Why Not MongoDB? Vs. ColdFusion 2 != “2” And fixing that is either: A) Ugly as hell: javacast(“int”, 2)… everywhere! B) slow(ish) if using CFMongoDB I get 1000 inserts in @1sec on a raggedy-ass laptop
60
Our Journey Pop Quiz Why MongoDB For CF-ers? The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
61
Awesome: Query Operators $ne: db.people.find( { NAME: { $ne: “Shaggy”} } ) $nin: db.people.find( { NAME: { $nin: [“Shaggy”, “Daphne”] } } ) $gt and $lt: db.people.find( AGE: { {$gt: 30, $lt: 35} } ) $size (eg, people with exactly 3 kids): db.people.find( KIDS: {$size: 3} ) regex: (eg, names starting with Ma or Mi) db.people.find( {NAME: /^M(a|i)/} ) More at http://www.10gen.com/referencehttp://www.10gen.com/reference
62
Awesome: Atomic Modifiers $inc db.pageviews.update( {URL: ‘http://myurl.com’}, {$inc: {N: 1}} ) $set db.people.update( {NAME: ‘Steve’}, {$set: {Age: 35}} ) $push (for atomically adding values to an array) db.people.update( {NAME: ‘Steve’}, {$push: {KIDS: {NAME: ‘Sylvia’, AGE: 3}}}) findAndModify() db.tasks.findAndModify( query: {STATUS: ‘pending’}, sort: {PRIORITY: -1}, update: {$set: {STATUS: ‘running’, TS: new Date()}} ) More at http://www.10gen.com/referencehttp://www.10gen.com/reference
63
Awesome: ObjectId BSON (binary-encoded JSON) Unique: combination of Time Machine Process id Incrementer Mongo will automatically add this _id field Language-specific drivers automatically add this, too (BEFORE saving!) Your documents get a timestamp for free CFMongoDB has a function that gives it to you
64
Awesome: The DOCS RTFM http://www.mongodb.org/display/DOCS/Home
65
Our Journey Pop Quiz Why MongoDB For CF-ers? The Feel of MongoDB Why MongoDB? Why Not MongoDB? Some more awesomeness CFMongoDB
66
CFMongoDB: Description and Goals Description Founded by Bill Shelton, MXUnit founder (@virtix on Twitter) https://github.com/marcesher/cfmongodb https://github.com/marcesher/cfmongodb Library for using MongoDB in CFML Supported on CF9.0.1 and Railo 3.2 Wraps around the Official MongoDB Java driver Goals “Smooth the rough edges” of CFML & MongoDB Query DSL for “joyful” querying and easier debugging 2 == “2”, dammit! “Struct transparency”: you save structs, you get structs Easy access to Java drivers so you can everything the Java drivers provide More to come (object persistence/mapping, etc)
67
CFMongoDB: Getting Started Download CFMongoDB and put in your webroot Install MongoDB and confirm it works It ships with the latest 10gen Java drivers It ships with Mark Mandel’s JavaLoader… you needn’t add any jars to your path Open examples/gettingstarted.cfm Inspect code Run code Rejoice
68
CFMongoDB: Init the Config object We need to create some Java objects. Who’s gonna do it? The “factory”: ooooooh, spoooooooky! In English: “Hey, ColdFusion”, create these java objects for me OR “Hey, JavaLoader, you create these objects cuz I don’t wanna put jars in my lib folder”
69
CFMongoDB: Init the Config object Typical CF You must put the mongo jar and cfmongodb jar in your cfusion/lib mongoConfig = createObject('component','cfmongodb.core.MongoConfig').init(dbName="mongorocks"); JavaLoader You don’t need to put any jars in your path javaloaderFactory = createObject('component','cfmongodb.core.JavaloaderFactory').init(); mongoConfig = createObject('component','cfmongodb.core.MongoConfig').init(dbName="mongorocks", mongoFactory=javaloaderFactory);
70
CFMongoDB: Init the Mongo object mongo = createObject('component','cfmongodb.core.Mongo').init(mongoConfig);
71
CFMongoDB: Saving
73
CFMongoDB: Querying
78
CFMongoDB: Search Result
79
CFMongoDB: Find by ObjectID
80
CFMongoDB: findAndModify()
82
CFMongoDB: Off you go! See cfmongodb/examples for search examples, master/detail examples, And more examples forthcoming
83
Resources http://www.mongodb.org/ http://www.mongodb.org/ Click “Try It Out” for an in-browser shell! http://www.mongodb.org/display/DOCS/Home http://www.mongodb.org/display/DOCS/Home Coding Docs: http://www.mongodb.org/display/DOCS/Developer+Zone http://www.mongodb.org/display/DOCS/Developer+Zone Admin Docs: http://www.mongodb.org/display/DOCS/Admin+Zonehttp://www.mongodb.org/display/DOCS/Admin+Zone Possibly the single best MongoDB resource next to the docs: http://www.markus-gattol.name/ws/mongodb.html http://www.markus-gattol.name/ws/mongodb.html More presentations http://www.mongodb.org/display/DOCS/Slide+Gallery http://www.mongodb.org/display/DOCS/Slide+Gallery Articles http://www.mongodb.org/display/DOCS/Articles http://www.mongodb.org/display/DOCS/Articles
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.