2. Library Choices Raw MongoDB Driver Map<String, Object> view of objects Rough but dynamic Morphia (type-safe mapper) POJOs Annotation based (similar to JPA) Syntactic sugar and helpers Others Code generators, other jvm languages
4. BSON Package Types int and long Array/ArrayList String byte[] – binData Double (IEEE 754 FP) Date (secs since epoch) Null Boolean JavaScript String Regex
6. Simple Example DBCollectioncoll = new Mongo().getDB(“test”); coll.save( new BasicDBObjectBuilder(“name”, “scott”). append(“sex”, “male”). append(“height”, 178)).get());
7. Simple Example, Again DBCollectioncoll = new Mongo().getDB(“test”); Map<String, Object> fields = new … fields.add(“name”, “scott”); fields.add(“sex”, “male”); fields.add(“height”, 178); coll.insert(new BasicDBObject(fields));
11. Maps of Maps Can represent object graph/tree Always keyed off String (field)
12. Morphia: MongoDB Mapper Maps POJO Type-safe Access Patterns: DAO/Datastore/??? Data Types Performs well JPA like Many concepts came from Objectify (GAE)
17. Add, Get, Delete Person me = new Person(“scott”, Sex.Male, 179) Datastoreds = new Morphia().createDatastore() ds.save(me); Person meAgain = ds.get(Person.class, “scott”) ds.delete(me);
18. Queries Datastoreds = … Query q = ds.createQuery(Person.class); q.field(“height”).greaterThan(155).limit(5); for(Person p : q.fetch()) print(p); Person me = q.field(“name”).startsWith(“sc”).get();
21. Relationships [@Embedded] Loaded/Saved with Entity Update @Reference Stored as DBRef(s) Loaded with Entity Not automatically saved Key<T> (DBRef) Stored as DBRef(s) Just a link, but resolvable by Datastore/Query