Webinar: Simplifying Persistence for Java and MongoDB
-
Upload
mongodb -
Category
Technology
-
view
1.766 -
download
0
description
Transcript of Webinar: Simplifying Persistence for Java and MongoDB
![Page 1: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/1.jpg)
Java Evangelist, 10gen
Jeff Yemin
Morphia: Simplifying Persistence for Java and MongoDB
![Page 2: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/2.jpg)
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
![Page 3: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/3.jpg)
Morphia
• Object Document Mapper– Specified with annotations– Implemented with reflection
• Fluent query and update APIs– Runtime validation
![Page 4: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/4.jpg)
Morphia by Example
• Model
• Test
• Output
![Page 5: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/5.jpg)
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.10.1</version></dependency>
<dependency> <groupId>com.google.code.morphia</groupId> <artifactId>morphia</artifactId> <version>0.99</version></dependency>
Dependencies
![Page 6: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/6.jpg)
<repository> <id>morphia</id> <name>Morphia</name> <url>http://morphia.googlecode.com/svn/mavenrepo/</url> <layout>default</layout></repository>
Repository
![Page 7: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/7.jpg)
Morphia morphia = new Morphia();Mongo mongo = new Mongo();Datastore ds = morphia.createDatastore(mongo, "test");
Create the Datastore
![Page 8: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/8.jpg)
Entity Modelling
![Page 9: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/9.jpg)
Let's model github
![Page 10: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/10.jpg)
class Programmer { String name;}
First Entity (model)
![Page 11: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/11.jpg)
Programmer programmer = new Programmer();programmer.name= "Scott Hernandez";
ds.save(programmer);
First Entity (test)
![Page 12: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/12.jpg)
> db.Programmer.findOne(){
"_id" : ObjectId("503292d51aa814c051554696"),"className" : "demo.Programmer","name" : "Scott Hernandez"
}
First Entity (shell)
![Page 13: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/13.jpg)
class Programmer { @Id ObjectId id; String name; public void toString() {…}}
@Id (model)
![Page 14: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/14.jpg)
Programmer programmer = new Programmer();programmer.name= "Scott Hernandez";
ds.save(programmer);System.out.println(programmer)
@Id (test)
![Page 15: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/15.jpg)
Programmer{id=5032935f1aa8a8aa3485b441, name='Scott Hernandez'}
@Id (toString)
![Page 16: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/16.jpg)
class Programmer { @Id String githubUserName; String name;}
String Id (model)
![Page 17: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/17.jpg)
Programmer programmer = new Programmer();programmer.githubUserName = "scotthernandez";programmer.name= "Scott Hernandez";
ds.save(programmer);
String Id (test)
![Page 18: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/18.jpg)
> db.Programmer.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","name" : "Scott Hernandez"
}
String Id (shell)
![Page 19: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/19.jpg)
@Entity("programmers")class Programmer { @Id String githubUserName; String name;}
@Entity (model)
![Page 20: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/20.jpg)
> db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","name" : "Scott Hernandez"
}
@Entity (shell)
![Page 21: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/21.jpg)
@Entity("programmers")class Programmer { @Id String githubUserName; String name; Date memberSince; boolean active; int followers;}
More primitives (model)
![Page 22: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/22.jpg)
Programmer scott = new Programmer();scott.userName = "scotthernandez";scott.name = "Scott Hernandez";scott.since = dateFmt.parse("Aug 12, 2009");scott.active = true;scott.followers = 8;
ds.save(scott);
More primitives (test)
![Page 23: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/23.jpg)
> db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","name" : "Scott Hernandez","memberSince" : ISODate("2009-08-12T04:00:00Z"),"active" : true,"followers" : 8
}
More primitives (shell)
![Page 24: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/24.jpg)
@Entity("programmers")class Programmer { @Id String githubUserName; String name; Date memberSince; boolean active; int followers; List<String> following;}
Primitive Array (Model)
![Page 25: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/25.jpg)
Programmer scott = new Programmer();scott.userName = "scotthernandez";scott.name = "Scott Hernandez";scott.since = dateFmt.parse("Aug 12, 2009");scott.active = true;scott.followers = 8;scott.following = Arrays.asList("moraes", "stickfigure");
ds.save(scott);
Primitive Array (test)
![Page 26: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/26.jpg)
db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","name" : "Scott Hernandez","memberSince" : ISODate("2009-08-12T04:00:00Z"),"active" : true,"followers" : 8,"following" : [
"moraes","stickfigure"
]}
Primitive Array (shell)
![Page 27: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/27.jpg)
@Entity("programmers")class Programmer { @Id String githubUserName; Name name; Date memberSince; boolean active; int followers; List<String> following;}
@Embeddedclass Name { String first, last;}
@Embedded (model)
![Page 28: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/28.jpg)
Programmer programmer = new Programmer();programmer.githubUserName = "scotthernandez";programmer.name = new Name("Scott", "Hernandez");programmer.memberSince = dateFmt.parse("Aug 12, 2009");programmer.active = true;programmer.followers = 8;programmer.following = Arrays.asList("moraes", "stickfigure");
ds.save(programmer);
@Embedded (test)
![Page 29: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/29.jpg)
> db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","name" : {
"first" : "Scott","last" : "Hernandez"
},"memberSince" : ISODate("2009-08-12T04:00:00Z"),"active" : true,"followers" : 8,"following" : [
"moraes","stickfigure"
]}
@Embedded (shell)
![Page 30: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/30.jpg)
@Entity("programmers")class Programmer { @Id String githubUserName; Name name; Date memberSince; boolean active; int followers; List<String> following; List<Repository> repositories;}
@Embeddedclass Name { String first, last;}
@Embeddedclass Repository { String name; String forkedFrom;}
@Embedded List (model)
![Page 31: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/31.jpg)
Programmer programmer = new Programmer();programmer.githubUserName = "scotthernandez";programmer.name = new Name("Scott", "Hernandez");programmer.memberSince = dateFmt.parse("Aug 12, 2009");programmer.active = true;programmer.followers = 8;programmer.following = Arrays.asList("moraes", "stickfigure");programmer.repositories = Arrays.asList( new Repository("docs", "mongodb/docs"), new Repository("mongo-java-driver", "mongodb/mongo-java-driver"));
ds.save(programmer);
@Embedded (test)
![Page 32: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/32.jpg)
> db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer",…"repositories" : [
{"name" : "docs","forkedFrom" : "mongodb/docs"
},{
"name" : "mongo-java-driver","forkedFrom" : "mongodb/mongo-java-
driver"}
]}
@Embedded List (shell)
![Page 33: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/33.jpg)
@Entity("repos")class Repository { @Id ObjectId id; @Reference Programmer owner; String name; String forkedFrom;}
@Reference (model)
![Page 34: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/34.jpg)
Programmer programmer = new Programmer();programmer.githubUserName = "scotthernandez";programmer.name = new Name("Scott", "Hernandez");programmer.memberSince = dateFmt.parse("Aug 12, 2009");programmer.active = true;programmer.followers = 8;programmer.following = Arrays.asList("moraes", "stickfigure");
ds.save(programmer);
Repository repo = new Repository(programmer, "docs", "mongodb/docs");
ds.save(repo);
@Reference (test)
![Page 35: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/35.jpg)
> db.repos.findOne(){
"_id" : ObjectId("503297e31aa8255abe542aaa"),"className" : "demo.Repository","owner" : DBRef("programmers", "scotthernandez"),"name" : "docs","forkedFrom" : "mongodb/docs"
}
@Reference (shell)
![Page 36: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/36.jpg)
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;}
Small schema change (model)
![Page 37: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/37.jpg)
Programmer scott = new Programmer();//…ds.save(scott);
// save mongodb OrganizationOrganization mongodb = new Organization("mongodb", "mongodb", sdf.parse("Jan 8, 2009"));ds.save(mongodb); // save mongodb's docs RepositoryRepository mongoDocs = new Repository(mongodb, "docs");ds.save(mongoDocs);
// save Scott's forked docs RepositoryRepository scottDocs = new Repository(scott, "docs", mongoDocs);ds.save(scottDocs);
Small schema change (test)
![Page 38: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/38.jpg)
> db.orgs.findOne(){
"_id" : "mongodb","className" : "demo.Organization","memberSince" : ISODate("2009-01-08T05:00:00Z"),"active" : false,"name" : "mongodb"
}> db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","memberSince" : ISODate("2009-08-12T04:00:00Z"),"active" : true,"name" : "Scott Hernandez"
…}
Small schema change (shell)
![Page 39: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/39.jpg)
> db.repos.find().toArray()[
{"_id" : ObjectId("503298be1aa8b1d255e5d45b"),"className" : "demo.Repository","owner" : DBRef("orgs", "mongodb"),"name" : "docs"
},{
"_id" : ObjectId("503298be1aa8b1d255e5d45c"),"className" : "demo.Repository","owner" : DBRef("programmers",
"scotthernandez"),"name" : "docs","forkedFrom" : DBRef("repos",
ObjectId("503298be1aa8b1d255e5d45b"))}
]
Small schema change (shell, 2)
![Page 40: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/40.jpg)
Querying
![Page 41: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/41.jpg)
Find by Equality (test)
• ds.get(Programmer.class, "scotthernandez")
• ds.find(Programmer.class, "name", "Scott Hernandez")
• ds.find(Programmer.class).field("name").equal("Scott Hernandez”)
![Page 42: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/42.jpg)
Find by Equality (logs)
• test.programmers query: { _id: "scotthernandez" }
• test.programmers query: { name: "Scott Hernandez" }
• test.programmers query: { name: "Scott Hernandez" }
![Page 43: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/43.jpg)
Find by Range (test)
• ds.find(Programmer.class).field("followers").greaterThan(0)
• ds.find(Programmer.class).filter("followers >", 0)
• ds.find(Programmer.class).field("memberSince").lessThan(sdf.parse("Jan 1,
2010"))
![Page 44: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/44.jpg)
Find by Range (logs)
• test.programmers query: { followers: { $gt: 0 } }
• test.programmers query: { followers: { $gt: 0 } }
• test.programmers query: { memberSince: { $lt: new
Date(1262322000000) } }
![Page 45: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/45.jpg)
ds.find(Programmer.class). field("memberSince").lessThan(dateFmt.parse("Jan 1, 2010")).
field("followers").greaterThan(0)
Combining conditions (test)
![Page 46: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/46.jpg)
test.programmers query: { memberSince: { $lt: new Date(1262322000000) }, followers: { $gt: 0 } }
Combining conditions(logs)
![Page 47: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/47.jpg)
Programmer scott = new Programmer("scotthernandez")ds.find(Repository.class).field("owner").equal(scott)
Find by Reference (test)
![Page 48: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/48.jpg)
test.repos query: { owner: { $ref: "programmers", $id: "scotthernandez" } }
Find by Reference (logs)
![Page 49: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/49.jpg)
Indexing
![Page 50: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/50.jpg)
@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 Repository forkedFrom;
![Page 51: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/51.jpg)
@Indexes and @Index
• @Indexes: Annotation for types– value (Index[])
• @Index– value (String)– Others same as @Indexed
• Examples– @Indexes(@Index("since, -followers")) public class
Programmer {…}
![Page 52: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/52.jpg)
Updating
![Page 53: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/53.jpg)
Programmer jeff = createJeff();ds.save(jeff);
// jeff is following scott, so increment // scott's followers and re-saveProgrammer scott = ds.get(Programmer.class, "scotthernandez")
scott.followers++;ds.save(scott);
Updating with save (test)
![Page 54: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/54.jpg)
update test.programmers
query: { _id: "scotthernandez", } update: { _id: "scotthernandez", className: "demo.Programmer", followers: 9, following: [ "moraes", "stickfigure" ], memberSince: new Date(1250049600000), active: true, name: "Scott Hernandez", }
Updating with save (logs)
![Page 55: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/55.jpg)
> db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","followers" : 9,"following" : [
"moraes","stickfigure"
],"memberSince" : ISODate("2009-08-12T04:00:00Z"),"active" : true,"name" : "Scott Hernandez"
}
Updating with save (shell)
![Page 56: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/56.jpg)
@Entitypublic abstract class Member { @Id String userName; @Property("memberSince") Date since; boolean active; String name; @Version Long version;}
Optimistic Concurrency (model)
![Page 57: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/57.jpg)
update test.programmers
query: { _id: "scotthernandez", version: 1345497713173 } update: { _id: "scotthernandez", className: "demo.Programmer", followers: 9, following: [ "moraes", "stickfigure" ], memberSince: new Date(1250049600000), active: true, name: "Scott Hernandez", version: 1345497718181 }
Optimistic Concurrency (logs)
![Page 58: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/58.jpg)
> db.programmers.findOne(){
"_id" : "scotthernandez","className" : "demo.Programmer","followers" : 9,"following" : [
"moraes","stickfigure"
],"memberSince" : ISODate("2009-08-12T04:00:00Z"),"active" : true,"name" : "Scott Hernandez","version" : NumberLong("1345497718181")
}
Optimistic Concurrency (shell)
![Page 59: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/59.jpg)
Programmer jeff = createJeff();ds.save(jeff);
// increment followers of scott by oneUpdateOperations<Programmer> incrementFollowing = ds.createUpdateOperations(Programmer.class). inc("followers", 1);
Query<Programmer> queryForScott = ds.find(Programmer.class, "_id", "scotthernandez");
ds.update(queryForScott, incrementFollowing);
UpdateOperations (test)
![Page 60: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/60.jpg)
update test.programmers
query: { _id: "scotthernandez" }
update: { $inc: { followers: 1 } }
UpdateOperations (logs)
![Page 61: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/61.jpg)
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
![Page 63: Webinar: Simplifying Persistence for Java and MongoDB](https://reader035.fdocuments.us/reader035/viewer/2022062703/5555a9d4d8b42a52568b4867/html5/thumbnails/63.jpg)
Engineering Manager, 10gen
Jeff Yemin
Thank You