MongoDB is the leading noSQL database and as with any new technology, some of the biggest questions are about tools and integration. This webinar will cover how to use MongoDB from a Java application and introduce Morphia, the object document mapping library. It will cover the essential features of the framework with examples before moving on to some of the more advanced and upcoming features such as text search and aggregation framework support.
3. What is Morphia?
•
Object document mapper — https://github.com/mongodb/morphia
•
Provides mapping to/from Java objects to mongodb
•
Annotation-based
•
Embedded objects/documents
•
References (lazy or eager)
•
Fluent query/update APIs
•
Runtime validation
4. Why Morphia?
•
Offload object marshaling
•
automatically keeps up as objects evolve: new, removed, renamed
•
Higher abstraction
•
Simplified management of indexes
•
Unify code with schema management
•
Object/Document Versioning
5. A Brief History
•
Created by Scott Hernandez
•
Hired by 10Gen —> started working on the kernel
•
Morphia left alone for a couple of years
•
Lots of uncertainty
•
James Green forked morphia
•
I was hired in June, 2013 in part to pick up Morphia
•
5 releases since with another pending
19. Complex Entities
@Embedded
public class Settings {
public String defaultBranch = "master";
public Boolean allowWiki = false;
public Boolean allowIssues = true;
}
20. Complex Entities
@Entity("repos")
public class Repository {
@Id
public String name;
@Reference
public Organization organization;
@Reference
public GithubUser owner;
public Settings settings = new Settings();
}
21. Complex Entities
@Entity(value = "users", noClassnameStored = true)
public class GithubUser {
@Id
public final String userName;
public String fullName;
@Property("since")
public Date memberSince;
@Reference(lazy = true)
public List<Repository> repositories = new
ArrayList<>();
}
30. Complex Entities
@Entity("repos")
public class Repository {
@Id
public String name;
@Reference
public Organization organization;
@Reference
public GithubUser owner;
public Settings settings = new Settings();
}
31. Basic Query — @Property
@Entity(value = "users", noClassnameStored = true)
public class GithubUser {
@Id
private String userName;
private String fullName;
@Property("since")
private Date memberSince;
}
41. Indexes — Classes
@Entity(value = "users", noClassnameStored = true)
@Indexes({
@Index(value ="userName, -followers", name = "popular")
})
public class GithubUser {
@Id
public String userName;
public String fullName;
@Property("since")
public Date memberSince;
public Date lastActive;
@Reference(lazy = true)
public List<Repository> repositories = new ArrayList<>();
public int followers = 0;
public int following = 0;
!
…