MongoDB Aug2010 SF Meetup
-
Upload
scott-hernandez -
Category
Technology
-
view
1.416 -
download
1
description
Transcript of MongoDB Aug2010 SF Meetup
MongoDB: ObjStorSol + Des+Map’n
Yeah, I shortened that… but not to OSSDnM
… AppServer - J2EE (JBoss, Resin) Hosted AppServices - Google AppEngine Datastore - Objectify (GAEJ ORM/ODM) MongoDB – Morphia (ODM)
Community Developer (and more)
How I got here…
Basic Expectations No transactions (but atomic updates) Memory mapped data files Document store Fast-N-Loose by default (no safe/w/GLE) Indexing order matters; query optimizer
helps One writer (serialized writes) One JS thread (M/R, $where, group) Many query threads (non-js) BSON everywhere
What is the shell?
•vars•function
s•data
structs + types
Embedded JavaScript Interpreter
•ObjectId("...")
•new Date()
•Object.bsonsize()
Global Functions and Objects
•db["collection"].find/count/update
•short-hand for collections
MongoDB driver Exposed
•Doesn't require quoted keys
•Don’t copy and paste too much
JSON-like stuff
What is it good for? Debugging Testing Administration Scripting Glue Not building apps, probably…
Shell Demo
Single Collection, Inheritance One – Many
◦ Embedded◦ Reference
On the one -> Array of refs On the Many -> Add a ref field
Trees Many – Many
◦ Prob. 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 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
Java Library Choices
BSON Package◦ Types◦ Encode/Decode◦ DBObject (Map<String, Object>)
Nested Maps Directly encoded to binary format (BSON)
MongoDB Package◦ Mongo◦ DBObject (BasicDBObject/Builder)◦ DB/DBColletion◦ DBQuery/DBCursor
MongoDB Java Driver
Data Types◦ int and long◦ Array/ArrayList◦ String◦ byte[] – binData◦ Double (IEEE 754 FP)◦ Date (ms since epoch UTC)◦ Null◦ Boolean◦ JavaScript String◦ Regex◦ ObjectId (ts(4)+ host(3) + pid(2)+ incr(3) ) 12-byte
BSON Package
Morphia: MongoDB Mapper Maps POJO Type-safe Access Patterns:
DAO/Datastore/RollYourOwn Data Types Low performance overhead JPA like
Many 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 @PostLoad
EntityListeners EntityInterceptor
Datastore Basics get(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;
}
Queries
Datastore ds = …
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 fields◦ Push/pop arrays (lists)◦ Increment numeric fields
◦ Any combination of the above
Get/Save or Update
Update Operations set(field, val) unset(field)
inc(field, [val]) dec(field)
add(field, val) addAdd(field, vals)
removeFirst/Last(field) removeAll(field, vals)
Update existing props Update w/new props
Examples
Update
Datastore ds = …Query q = ds.find(Person.class, “name”, “scott”);UpdateOperation uo =
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 Entity◦ Update
@Reference◦ Stored as DBRef(s)◦ Loaded with Entity◦ Not automatically saved
Key<T>◦ Stored as DBRef(s)◦ Just a link, but resolvable by Datastore/Query
Questions?