Your SlideShare is downloading. ×

Java development with MongoDB


Published on

Presented at MongoSF on April 30, 2010.

Presented at MongoSF on April 30, 2010.

Published in: Technology
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Java Development with MongoDB James Williams Software Engineer, BT/Ribbit
  • 2. Agenda Java Driver basics Making Connections Managing Collections BasicDBObjectBuilder Document Queries GridFS Morphia Beyond the Java language Groovy utilities Grails plugin
  • 3. Making a Connection import com.mongodb.Mongo; import com.mongodb.DB; Mongo m = new Mongo(); Mongo m = new Mongo( "localhost" ); Mongo m = new Mongo( "localhost" , 27017 ); DB db = m.getDB( "mydb" );
  • 4. Working with Collections Getting all collections in the database Set<String> colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s); } Getting a single collection DBCollection coll = db.getCollection("testCollection")
  • 5. Inserting Documents BasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1); BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102); doc.put("info", info); coll.insert(doc);
  • 6. BasicDBObjectBuilder Utility for building objects Can coerce Maps (and possibly JSON*) to DBObjects Example: BasicDBObjectBuilder.start() .add( "name" , "eliot" ) .add( "number" , 17 ) .get();
  • 7. Document Queries DBObject myDoc = coll.findOne(); // can also use BasicDBObject query = new BasicDBObject(); query.put("i", 71); DBCursor cur = coll.find(query);
  • 8. GridFS mechanism for storing files larger than 4MB files are chunked allowing fetching of a portion or out of order chunking is mostly transparent to underlying operating system can store files in buckets, a MongoDB metaphor for folders default is the fs bucket
  • 9. Saving a file to GridFS def mongo = new Mongo(host) def gridfs = new GridFS(mongo.getDB("db")) def save(inputStream, contentType, filename) { def inputFile = gridfs.createFile(inputStream) inputFile.setContentType(contentType) inputFile.setFilename(filename) }
  • 10. Retrieving/Deleting a file def retrieveFile(String filename) { return gridfs.findOne(filename) } def deleteFile(String filename) { gridfs.remove(filename) }
  • 11. Morphia Apache 2 Licensed brings Hibernate/JPA paradigms to MongoDB allows annotating of POJOs to make converting them between MongoDB and Java very easy supports DAO abstractions offers type-safe query support compatible with GWT, Guice, Spring, and DI frameworks
  • 12. Morphia Annotations @Id @Entity @Embedded @Reference @Indexed @Serialized @Property
  • 13. Creating a Morphia POJO import*; @Entity("collectionName") public class Contact { @Id private String id; //generated by MongoDB private String firstName; private String lastName; @Embedded private List<PhoneNumber> phoneNumbers; // getters and setters }
  • 14. Mapping a POJO to a Mongo doc Morphia morphia = ...; Mongo mongo = ...; DB db = mongo.getDB("contacts"); Contact contact = ...; // map the contact to a DBObject DBObject contactObj = morphia.toDBObject(contact); db.getCollection("personal").save(contactObj);
  • 15. Getting a POJO from a Mongo doc Morphia morphia = ...; Mongo mongo = ...; DB db = mongo.getDB("contacts"); String contactId = ...; //load the object from the collection BasicDBObject idObj = new BasicDBObject( "_id", new ObjectId(contactId) ); BasicDBObject obj = (BasicDBObject) db.getCollection("personal").findOne(idObj); Contact contact = morphia.fromDBObject(Contact.class, obj);
  • 16. DAOs Encapsulate saving and retrieving objects Auto-converts to and from POJOs Can provide constraints on searches Key functions: get(<mongoId>) find() or find(constraints) findOne(constraints) deleteById(<mongoId>)
  • 17. DAO Example import com.mongodb.Mongo import* class EntryDAO extends DAO<BlogEntry,String> { public EntryDAO(Morphia morphia, Mongo mongo) { super(mongo, morphia, "entries") } }
  • 18. Constraints Examples dao.find(new Constraints() . orderByDesc("dateCreated") ).asList() dao.find( new Constraints() .field("dateCreated").greaterThanOrEqualTo(date).field("title"). equalTo(params.title) ).asList()
  • 19. Beyond the Java Language
  • 20. MongoDB with Groovy Metaprogramming with MongoDB can reduce LOC Dynamic finders Fluent interface mirroring Ruby and Python
  • 21. Groovy + MongoDB Java BasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1); coll.insert(doc); Groovier def doc = [name:"MongoDB",type:"database", count:1, info: [x:203, y:102] ] as BasicDBObject coll.insert(doc) Grooviest (using groovy-mongo) coll.insert([name:"MongoDB", type:"database", info: [x:203, y:102]])
  • 22. Dynamic Finders can build complex queries at runtime can even reach into objects for addition query parameters Ex. collection.findByAuthorAndPostCreatedGreaterThan(...) collection.findByComments_CreatedOn(...)
  • 23. How dynamic finders work Groovy receives the request for the method The method is not found (invoking methodMissing) The method name used to construct a query template The method is cached The method is invoked with the parameters Future invocations use the cached method
  • 24. MongoDB Grails Plugin Replaces JDBC layer in Grails applications Can use dynamic finders Requires only slight modifications to domain classes
  • 25. Links Personal Blog: Twitter: Morphia: Utilities for Groovy: MongoDB Grails plugin: http://github. com/mpriatel/mongodb-grails