3. MongoDB
• Document-oriented storage
– JSON-style documents
• Rich, document-based queries
• Full index support
• Fast in-place updates
• Convenient Javascript shell
• Customer drivers for all major language
4. MongoDB on the JVM
• MongoDB Java Driver
– Map-based API
• JVM language integrations
– Casbah (Scala)
– Jmongo (Ruby)
– Monger (Clojure)
• ODM (Object Document Mapper)
– Morphia
– Spring Data MongoDB
5. Morphia
• Object Document Mapper
– Specifed with annotations
– Implemented with reflection
• Fluent query and update APIs
– Runtime validation
36. Small schema change (model)
abstract class Member {
@Id String userName;
@Property("memberSince") Date since;
boolean active;
String name;
}
@Entity("programmers")
class Programmer extends Member {
int followers;
List<String> following;
}
@Entity("orgs")
class Organization extends Member {
}
@Entity("repos")
class Repository {
@Id ObjectId id;
@Reference Member owner;
String name;
@Reference(lazy=true) Repository forkedFrom;
}
37. Small schema change (test)
Programmer scott = new Programmer();
//…
ds.save(scott);
// save mongodb Organization
Organization mongodb = new Organization("mongodb",
"mongodb", sdf.parse("Jan 8, 2009"));
ds.save(mongodb);
// save mongodb's docs Repository
Repository mongoDocs = new Repository(mongodb, "docs");
ds.save(mongoDocs);
// save Scott's forked docs Repository
Repository scottDocs = new Repository(scott, "docs",
mongoDocs);
ds.save(scottDocs);
48. @Indexed
• Annotation for fields
– value (IndexDirection)
– name (String)
– unique (boolean)
– dropDups (boolean)
– background (boolean)
– sparse (boolean)
• Examples
– @Indexed(value=IndexDirection.ASC, name="followers") int followers;
– @Indexed @Reference(lazy = true) Repository forkedFrom;
49. @Indexes and @Index
• @Indexes: Annotation for types
– value (Index[])
• @Index
– value (String)
– Others same as @Indexed
• Examples
– @Indexes(@Index("since, -followers")) public class Programmer {…}
50. Updating with save (test)
Programmer jeff = createJeff();
ds.save(jeff);
// jeff is following scott, so increment
// scott's followers and re-save
Programmer scott = ds.get(Programmer.class,
"scotthernandez")
scott.followers++;
ds.save(scott);
53. Optimistic Concurrency (model)
@Entity
public abstract class Member {
@Id String userName;
@Property("memberSince") Date since;
boolean active;
String name;
@Version Long version;
}
56. UpdateOperations (test)
Programmer jeff = createJeff();
ds.save(jeff);
// increment followers of scott by one
UpdateOperations<Programmer> incrementFollowing =
ds.createUpdateOperations(Programmer.class).
inc("followers", 1);
Query<Programmer> queryForScott =
ds.find(Programmer.class, "userName", "scotthernandez");
ds.update(queryForScott, incrementFollowing);
57. Web Resources
• Morphia home: http://code.google.com/p/morphia/
• Morphia user group:
https://groups.google.com/forum/?fromgroups#!forum/morphia
• Demo code: https://github.com/jyemin/morphia-demo
– Separate commit and tag for each slide, so you
can play along
58. Boston Resources
• MongoDB User Group: http://www.meetup.com/Boston-
MongoDB-User-Group/
• 10gen office hours: Monthly at Dog Patch (with yours truly)
59. MongoDB Boston
• October 23-24
– Oct 23: Workshops on Schema Design and Operations
– Oct 24: Conference talks
– Discount code: MUG10 for 10% off tickets
– Early bird prices end Sept. 24
– Submit a talk proposal at 10gen.com/talk-proposal
– URL: 10gen.com/events/mongodb-boston