MongoDB: ObjStorSol + Des+Map’nYeah, I shortened that… but not to OSSDnM
…AppServer - J2EE (JBoss, Resin)Hosted AppServices - Google AppEngineDatastore - Objectify (GAEJ ORM/ODM)MongoDB – Morphia (ODM)Community Developer (and more)How I got here…
Basic ExpectationsNo transactions (but atomic updates)Memory mapped data filesDocument storeFast-N-Loose by default (no safe/w/GLE)Indexing order matters; query optimizer helpsOne writer (serialized writes)One JS thread (M/R, $where, group)Many query threads (non-js)BSON everywhere
What is the shell?
What is it good for?DebuggingTestingAdministrationScripting GlueNot building apps, probably…
Shell Demo
Single Collection, InheritanceOne – ManyEmbeddedReferenceOn the one -> Array of refsOn the Many -> Add a ref fieldTreesMany – ManyProb. best to store on only one side.Designs
{        _id : "scotthernandez",        name : "Scott Hernandez",desc : "that guy",        groups : [                "users",                "admins",                "fishermen"        ]}People - Groups
{        "_id" : "users",        "desc" : "normal users“ },{        "_id" : "fishermen",        "desc" : "people who fish“ },{        "_id" : "admins",        "privledges" : 2,        "desc" : "administrative users“ }Group - People
blog =: { text: “I like to swim”, author: “scott”,	comments: [		{author:”ralph”, text:”me2”, 		replies:[		   {author:”liz”, text:”doesn’t every1?”}		]},		{author:”jeff”, text:”good to know”}	],ts:Date(…)}Tree
Raw MongoDB DriverMap<String, Object> view of objectsRough but dynamicMorphia (type-safe mapper)POJOsAnnotation based (similar to JPA)Syntactic sugar and helpersOthersCode generators, other jvm languagesJava Library Choices
BSON PackageTypesEncode/DecodeDBObject (Map<String, Object>)Nested MapsDirectly encoded to binary format (BSON)MongoDB PackageMongoDBObject (BasicDBObject/Builder)DB/DBColletionDBQuery/DBCursorMongoDB Java Driver
Data Typesint and longArray/ArrayListStringbyte[] – binDataDouble (IEEE 754 FP)Date (ms since epoch UTC)NullBooleanJavaScript StringRegexObjectId (ts(4)+ host(3) + pid(2)+ incr(3) ) 12-byteBSON Package
Morphia: MongoDB MapperMaps POJOType-safeAccess Patterns: DAO/Datastore/RollYourOwnData TypesLow performance overheadJPA likeMany concepts came from Objectify (GAE)
Annotations@Entity(“collectionName”)@Id@Transient (not transient)@Indexed(…)@Property(“fieldAlias”)@AlsoLoad({aliases})@Reference@Serialized[@Embedded]
Lifecycle Events@PrePersist@PreSave@PostPersist@PreLoad@PostLoadEntityListenersEntityInterceptor
Datastore Basicsget(class, id)find(class, […])save(entity, […])delete(query)getCount(query)update/First(query, upOps)findAndModify/Delete(query, upOps)
Basic POJO@Entityclass Person {	@Id ObjectId name;SexEnum sex;@IndexedInteger height;}
QueriesDatastoreds = …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();
Save whole object graphs (get/save)Update parts (embedded objects)Un/set fieldsPush/pop arrays (lists)Increment numeric fieldsAny combination of the aboveGet/Save or Update
Update Operationsset(field, val)unset(field)inc(field, [val])dec(field)add(field, val)addAdd(field, vals)removeFirst/Last(field)removeAll(field, vals)
Update existing propsUpdate w/new propsExamples
UpdateDatastoreds = …Query q = ds.find(Person.class, “name”, “scott”);UpdateOperationuo = ds.createUpdateOperations(cls)uo.set(“city”, “seattle”).set(“lastUpdated”, new Date());UpdateResults res = ds.update(q, uo);if(res.getUpdatedCount() > 0)  //do something?
Morphia Relationships Annotations[@Embedded]Load/Save with EntityUpdate@ReferenceStored as DBRef(s)Loaded with EntityNot automatically savedKey<T>Stored as DBRef(s)Just a link, but resolvable by Datastore/Query
Or… ScottHernandez@gmail.comQuestions?

MongoDB Aug2010 SF Meetup