Your SlideShare is downloading. ×
0
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
San Francisco Java User Group
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

San Francisco Java User Group

3,408

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,408
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
45
Comments
0
Likes
0
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. MongoDB Kristina Chodorow September 8th, 2009
  • 2. Who am I? • Software Engineer at 10gen • Java, Perl, PHP, drivers
  • 3. Technique #1: literally scale
  • 4. Technique #1: literally scale (Courtesy of Ask Bjorn Hansen) $3,200 x 1250 = $4,000,000
  • 5. Technique #2: shard now-yesterday yesterday- the day before- the day before the day before that
  • 6. Technique #3: master-slave replication R W W R/W? W R
  • 7. Key/Value Stores • Project Voldemort • Tokyo Cabinet Graph DBs Fewer More Document-oriented DBs Relational DBs • MongoDB Cassandra • CouchDB
  • 8. MongoDB
  • 9. MongoDB • Ease of use • Scalable • Dynamic queries - similar “feel” to SQL • Speed of key/value stores (almost) • Power of RDBMSs (almost)
  • 10. Downloading MongoDB www.mongodb.org Binaries available for Linux, Mac, Windows, Solaris
  • 11. The Venerable Java Driver Available at Github: www.github.com/mongodb/mongo-java-driver/downloads JavaScript: { x : y, z : w } Java: BasicDBObjectBuilder.start().add("x", "y").add("z", "w").get()
  • 12. Connecting to the Database Mongo connection = new Mongo(“blog”); DBCollection collection = connection.getCollection(“posts”);
  • 13. Inserting collection.insert({ title : 'My first blog post', author : 'Joe', content : 'Hello, world!', comments : [] });
  • 14. Creating a User DBObject post = BasicDBObjectBuilder.start() .add("title", "My First Blog Post") .add("username", "Joe") .add("content", "Hello, world!") .add("comments", new ArrayList()) .get(); collection.insert(post);
  • 15. MongoDB::OID an autogenerated primary key collection.insert(post); System.out.println(post.get("_id")); -------------------------------------------- 4a9700dba5f9107c5cbc9a9c
  • 16. Updating collection.update({_id : post._id}, {$push : {comments : { author => 'Fred', comment => 'Dumb post.' } }}); { title : 'My first blog post', author : 'Joe', content : 'Hello, world!', comments : [{author : 'Fred', comment : 'Dumb post.'}] }
  • 17. Updating DBObject postId = BasicDBObjectBuilder.start() .add("_id", post.get("_id")).get(); DBObject comment = BasicDBObjectBuilder.start() .add("author", "Fred") .add("comment", "Dumb post.").get(); DBObject addComment = new BasicDBObject( "$push", new BasicDBObject("comments", comment)); collection.update(joeId, addComment);
  • 18. Magic $ $gt, $gte, $lt, $lte, $eq, $neq, $exists, $set, $mod, $where, $in, $nin, $inc $push, $pull, $pop, $pushAll, $popAll collection.find({ x : {$gt : 4}})
  • 19. Querying commentsByFred = collection.find({ "comments.author" : "Fred" }); commentsByFred = collection.find({ "comments.author" : /fred/i });
  • 20. Querying Pattern fredPattern = Pattern .compile("fred", CASE_INSENSITIVE); DBObject fredQuery = BasicDBObject.start() .add("comments.author", fredPattern) .get();
  • 21. $where collection.find({$where : 'this.y == (this.x + this.z)'}); Will work: {x : 1, y : 4, z : 3} {x : "hi", y : "hibye", z : "bye"} Won’t work: {x : 1, y : 1}
  • 22. Optimizing $where collection.find({ name : Sally, age : { $gt : 18}, $where : 'Array.sort(this.interests)[2] == "volleyball"'})
  • 23. Speaking of indexing… collection.ensureIndex({age : 1}); collection.ensureIndex({ "name" : 1, "ts" : -1, "comments.author" : 1 });
  • 24. Cursors DBCursor cursor = collection.find(criteria); while (cursor.hasNext()) { DBObject obj = cursor.next(); ... }
  • 25. Paging DBObject sort = BasicDBObjectBuilder.start() .add("ts", -1) .get(); DBCursor cursor = coll.find() .sort(sort) .skip(pageNum * resultsPerPage) .limit(resultsPerPage); List<DBObject> page = cursor.toArray();
  • 26. Twitter Schema user = { _id : ObjectId username : String, following : [] } tweet = { _id : ObjectId userId : ObjectId, msg : String, ts: Date }
  • 27. Following db.users.update({_id : joe._id}, {$push : { following : fred._id }}); { _id : "a3845e28c36e22e578bb243f", username : "joe", following : ["2e893d7a458c271ba2e0349e"] }
  • 28. Finding Followers db.users.find({$in : {following : joe._id}}, {username : 1}); Alice Bob Fred ...
  • 29. Storing Files 4 megabytes
  • 30. Storing Files
  • 31. Storing Files
  • 32. Storing Files ObjectId fileId = new ObjectId(); fileObj = { _id : fileId, filename : "ggbridge.png", user : "joe", takenIn : "San Francisco" } chunkObj = { fileId : fileId, chunkNum : N }
  • 33. Storing Files import com.mongodb.gridfs.*; GridFS fs = new GridFS(connection); GridFSFile file = fs.createFile(filename); file.put("user", "Joe"); ... fs.save(file);
  • 34. Retrieving Files GridFSFile file = fs.findOne(); file.writeTo("myfile.png");
  • 35. Logging • insert/update/remove is fast • Capped collections • Upserts • $inc for counts
  • 36. Thank you! kristina@10gen.com @kchodorow @mongodb irc.freenode.net#mongodb www.mongodb.org

×