Java Development with MongoDB - Mongo NYC 2010

58
Java Development with MongoDB Brendan W. McAdams Novus Partners, Inc. http://novus.com May 2010 / Mongo NYC Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 1 / 44

description

My slides for my talk at MongoNYC 2010 on Java Development

Transcript of Java Development with MongoDB - Mongo NYC 2010

Page 1: Java Development with MongoDB - Mongo NYC 2010

Java Development with MongoDB

Brendan W. McAdams

Novus Partners, Inc.http://novus.com

May 2010 / Mongo NYC

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 1 / 44

Page 2: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 2 / 44

Page 3: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 3 / 44

Page 4: Java Development with MongoDB - Mongo NYC 2010

Adding the MongoDB Driver To Your Project

Assuming you’re using a dependency manager, make setup simple. . .

Listing 1: Maven Dependency<dependency>

<groupId >org . mongodb</ groupId >< a r t i f a c t I d >mongo−java−d r i ve r </ a r t i f a c t I d ><vers ion >1.4 </ vers ion >

</dependency>

Listing 2: Ivy Dependency<dependency org=" org . mongodb"

name="mongo−java−d r i v e r "rev=" 1.4 " / >

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 5 / 44

Page 5: Java Development with MongoDB - Mongo NYC 2010

Adding the MongoDB Driver To Your Project

Assuming you’re using a dependency manager, make setup simple. . .

Listing 3: Maven Dependency<dependency>

<groupId >org . mongodb</ groupId >< a r t i f a c t I d >mongo−java−d r i ve r </ a r t i f a c t I d ><vers ion >1.4 </ vers ion >

</dependency>

Listing 4: Ivy Dependency<dependency org=" org . mongodb"

name="mongo−java−d r i v e r "rev=" 1.4 " / >

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 5 / 44

Page 6: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 6 / 44

Page 7: Java Development with MongoDB - Mongo NYC 2010

Simple Connection

Getting connected to MongoDB is simple; Connections are pooled, soyou only need one. . .impor t com. mongodb . Mongo ;impor t com. mongodb .DB;Mongo m = new Mongo ( ) ;Mongo m = new Mongo( " l o c a l h o s t " ) ;Mongo m = new Mongo( " l o c a l h o s t " , 27017) ;

Fetch a Database Handle (lazy). . .DB db = m. getDB ( " javaDemo " ) ;

If you need to authenticate. . .boolean auth = db . au then t i ca te ( " l o g i n " , " password " ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 8 / 44

Page 8: Java Development with MongoDB - Mongo NYC 2010

Simple Connection

Getting connected to MongoDB is simple; Connections are pooled, soyou only need one. . .impor t com. mongodb . Mongo ;impor t com. mongodb .DB;Mongo m = new Mongo ( ) ;Mongo m = new Mongo( " l o c a l h o s t " ) ;Mongo m = new Mongo( " l o c a l h o s t " , 27017) ;

Fetch a Database Handle (lazy). . .DB db = m. getDB ( " javaDemo " ) ;

If you need to authenticate. . .boolean auth = db . au then t i ca te ( " l o g i n " , " password " ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 8 / 44

Page 9: Java Development with MongoDB - Mongo NYC 2010

Simple Connection

Getting connected to MongoDB is simple; Connections are pooled, soyou only need one. . .impor t com. mongodb . Mongo ;impor t com. mongodb .DB;Mongo m = new Mongo ( ) ;Mongo m = new Mongo( " l o c a l h o s t " ) ;Mongo m = new Mongo( " l o c a l h o s t " , 27017) ;

Fetch a Database Handle (lazy). . .DB db = m. getDB ( " javaDemo " ) ;

If you need to authenticate. . .boolean auth = db . au then t i ca te ( " l o g i n " , " password " ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 8 / 44

Page 10: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 9 / 44

Page 11: Java Development with MongoDB - Mongo NYC 2010

Working with Collections

Collections are MongoDB "tables". . .List all of the collections in a database. . .Set<St r ing > = c o l l s = db . getCol lect ionNames ( ) ;f o r ( S t r i n g s : c o l l s ) {

System . out . p r i n t l n ( s ) ;}

Get a specific collection (lazy). . .DBCol lect ion c o l l = db . g e t C o l l e c t i o n ( " tes tData " ) ;

Count the number of documents in a collection. . .c o l l . getCount ( ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 11 / 44

Page 12: Java Development with MongoDB - Mongo NYC 2010

Working with Collections

Collections are MongoDB "tables". . .List all of the collections in a database. . .Set<St r ing > = c o l l s = db . getCol lect ionNames ( ) ;f o r ( S t r i n g s : c o l l s ) {

System . out . p r i n t l n ( s ) ;}

Get a specific collection (lazy). . .DBCol lect ion c o l l = db . g e t C o l l e c t i o n ( " tes tData " ) ;

Count the number of documents in a collection. . .c o l l . getCount ( ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 11 / 44

Page 13: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsBSON

"Documents" are MongoDB’s "rows".MongoDB’s Internal Document representation is ’BSON’

I BSON is a binary optimized flavor of JSONI Corrects JSON’s inefficiency in string encoding (Base64)I Supports extras including Regular Expressions, Byte Arrays,

DateTimes & Timestamps, as well as datatypes for Javascript codeblocks & functions.

I Creative Commons licensed.I BSON implementation being split into its own package in most

drivers.I bsonspec.org

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 12 / 44

Page 14: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObject

Java representation of BSON is the map-like DBObject (Java 2.0driver has a new base class of BSONObject related to the BSONsplit-off)Easiest way to work with Mongo Documents is BasicDBObject. . .

I BasicDBObject implements java.util.LinkedHashMap<String,Object>

I Mutable objectI Can take a Map as a constructor parameterI Example:

DBObject doc = new BasicDBObject ( ) ;doc . put ( " username " , "bwmcadams" ) ;doc . put ( " password " , "MongoNYC" ) ;

I toString returns a JSON serialization.

Use BasicDBList (implements java.util.ArrayList<Object>) torepresent Arrays.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44

Page 15: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObject

Java representation of BSON is the map-like DBObject (Java 2.0driver has a new base class of BSONObject related to the BSONsplit-off)Easiest way to work with Mongo Documents is BasicDBObject. . .

I BasicDBObject implements java.util.LinkedHashMap<String,Object>

I Mutable objectI Can take a Map as a constructor parameterI Example:

DBObject doc = new BasicDBObject ( ) ;doc . put ( " username " , "bwmcadams" ) ;doc . put ( " password " , "MongoNYC" ) ;

I toString returns a JSON serialization.

Use BasicDBList (implements java.util.ArrayList<Object>) torepresent Arrays.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44

Page 16: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObject

Java representation of BSON is the map-like DBObject (Java 2.0driver has a new base class of BSONObject related to the BSONsplit-off)Easiest way to work with Mongo Documents is BasicDBObject. . .

I BasicDBObject implements java.util.LinkedHashMap<String,Object>

I Mutable objectI Can take a Map as a constructor parameterI Example:

DBObject doc = new BasicDBObject ( ) ;doc . put ( " username " , "bwmcadams" ) ;doc . put ( " password " , "MongoNYC" ) ;

I toString returns a JSON serialization.

Use BasicDBList (implements java.util.ArrayList<Object>) torepresent Arrays.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44

Page 17: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObject

Java representation of BSON is the map-like DBObject (Java 2.0driver has a new base class of BSONObject related to the BSONsplit-off)Easiest way to work with Mongo Documents is BasicDBObject. . .

I BasicDBObject implements java.util.LinkedHashMap<String,Object>

I Mutable objectI Can take a Map as a constructor parameterI Example:

DBObject doc = new BasicDBObject ( ) ;doc . put ( " username " , "bwmcadams" ) ;doc . put ( " password " , "MongoNYC" ) ;

I toString returns a JSON serialization.

Use BasicDBList (implements java.util.ArrayList<Object>) torepresent Arrays.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44

Page 18: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObject

Java representation of BSON is the map-like DBObject (Java 2.0driver has a new base class of BSONObject related to the BSONsplit-off)Easiest way to work with Mongo Documents is BasicDBObject. . .

I BasicDBObject implements java.util.LinkedHashMap<String,Object>

I Mutable objectI Can take a Map as a constructor parameterI Example:

DBObject doc = new BasicDBObject ( ) ;doc . put ( " username " , "bwmcadams" ) ;doc . put ( " password " , "MongoNYC" ) ;

I toString returns a JSON serialization.

Use BasicDBList (implements java.util.ArrayList<Object>) torepresent Arrays.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44

Page 19: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObject

Java representation of BSON is the map-like DBObject (Java 2.0driver has a new base class of BSONObject related to the BSONsplit-off)Easiest way to work with Mongo Documents is BasicDBObject. . .

I BasicDBObject implements java.util.LinkedHashMap<String,Object>

I Mutable objectI Can take a Map as a constructor parameterI Example:

DBObject doc = new BasicDBObject ( ) ;doc . put ( " username " , "bwmcadams" ) ;doc . put ( " password " , "MongoNYC" ) ;

I toString returns a JSON serialization.

Use BasicDBList (implements java.util.ArrayList<Object>) torepresent Arrays.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44

Page 20: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObject

Java representation of BSON is the map-like DBObject (Java 2.0driver has a new base class of BSONObject related to the BSONsplit-off)Easiest way to work with Mongo Documents is BasicDBObject. . .

I BasicDBObject implements java.util.LinkedHashMap<String,Object>

I Mutable objectI Can take a Map as a constructor parameterI Example:

DBObject doc = new BasicDBObject ( ) ;doc . put ( " username " , "bwmcadams" ) ;doc . put ( " password " , "MongoNYC" ) ;

I toString returns a JSON serialization.

Use BasicDBList (implements java.util.ArrayList<Object>) torepresent Arrays.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44

Page 21: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObjectBuilder

For those who prefer immutability. . .BasicDBObjectBuilder follows the Builder pattern.add() your keys & values:BasicDBObjectBui lder b u i l d e r = BasicDBObjectBui lder . s t a r t ( ) ;b u i l d e r . add ( " username " , "bwmcadams" ) ;b u i l d e r . add ( " password " , "MongoNYC" ) ;b u i l d e r . add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) ;

A BasicDBObjectBuilder is not a DBObject.Call get() to return the built-up DBObject.add() returns itself so you can chain calls instead:BasicDBOBjectBui lder . s t a r t ( ) . add ( " username " , "bwmcadams" ) . add ( "

password " , "MongoNYC" ) . add ( " p resen ta t i on " , " Java Developmentw i th MongoDB" ) . get ( ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44

Page 22: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObjectBuilder

For those who prefer immutability. . .BasicDBObjectBuilder follows the Builder pattern.add() your keys & values:BasicDBObjectBui lder b u i l d e r = BasicDBObjectBui lder . s t a r t ( ) ;b u i l d e r . add ( " username " , "bwmcadams" ) ;b u i l d e r . add ( " password " , "MongoNYC" ) ;b u i l d e r . add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) ;

A BasicDBObjectBuilder is not a DBObject.Call get() to return the built-up DBObject.add() returns itself so you can chain calls instead:BasicDBOBjectBui lder . s t a r t ( ) . add ( " username " , "bwmcadams" ) . add ( "

password " , "MongoNYC" ) . add ( " p resen ta t i on " , " Java Developmentw i th MongoDB" ) . get ( ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44

Page 23: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObjectBuilder

For those who prefer immutability. . .BasicDBObjectBuilder follows the Builder pattern.add() your keys & values:BasicDBObjectBui lder b u i l d e r = BasicDBObjectBui lder . s t a r t ( ) ;b u i l d e r . add ( " username " , "bwmcadams" ) ;b u i l d e r . add ( " password " , "MongoNYC" ) ;b u i l d e r . add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) ;

A BasicDBObjectBuilder is not a DBObject.Call get() to return the built-up DBObject.add() returns itself so you can chain calls instead:BasicDBOBjectBui lder . s t a r t ( ) . add ( " username " , "bwmcadams" ) . add ( "

password " , "MongoNYC" ) . add ( " p resen ta t i on " , " Java Developmentw i th MongoDB" ) . get ( ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44

Page 24: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsFrom Java: BasicDBObjectBuilder

For those who prefer immutability. . .BasicDBObjectBuilder follows the Builder pattern.add() your keys & values:BasicDBObjectBui lder b u i l d e r = BasicDBObjectBui lder . s t a r t ( ) ;b u i l d e r . add ( " username " , "bwmcadams" ) ;b u i l d e r . add ( " password " , "MongoNYC" ) ;b u i l d e r . add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) ;

A BasicDBObjectBuilder is not a DBObject.Call get() to return the built-up DBObject.add() returns itself so you can chain calls instead:BasicDBOBjectBui lder . s t a r t ( ) . add ( " username " , "bwmcadams" ) . add ( "

password " , "MongoNYC" ) . add ( " p resen ta t i on " , " Java Developmentw i th MongoDB" ) . get ( ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44

Page 25: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsImplementation by Extension: DBObject

If you want to create your own concrete objects, extend &implement DBObject

I Requires you implement a map-like interface including ability to get& set fields by key (even if you don’t use them, required todeserialize)

I Instances of DBObject can be saved directly to MongoDB.Feeling Fancy? Reflect instead. . .

I Use ReflectionDBObject as a base class for your Beans.I ReflectionDBObject uses reflection to proxy your getters & setters

and behave like a DBObject.I Downside: With Java’s single inheritance you are stuck using this

as your base class.Existing Object Model? ORM-Like Solutions. . .

I Morphia (uses JPA-style annotations)I Daybreak (annotation based)I Mungbean (more Java-ey query syntax & PoJo Mapping)

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 17 / 44

Page 26: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsImplementation by Extension: DBObject

If you want to create your own concrete objects, extend &implement DBObject

I Requires you implement a map-like interface including ability to get& set fields by key (even if you don’t use them, required todeserialize)

I Instances of DBObject can be saved directly to MongoDB.Feeling Fancy? Reflect instead. . .

I Use ReflectionDBObject as a base class for your Beans.I ReflectionDBObject uses reflection to proxy your getters & setters

and behave like a DBObject.I Downside: With Java’s single inheritance you are stuck using this

as your base class.Existing Object Model? ORM-Like Solutions. . .

I Morphia (uses JPA-style annotations)I Daybreak (annotation based)I Mungbean (more Java-ey query syntax & PoJo Mapping)

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 17 / 44

Page 27: Java Development with MongoDB - Mongo NYC 2010

MongoDB DocumentsImplementation by Extension: DBObject

If you want to create your own concrete objects, extend &implement DBObject

I Requires you implement a map-like interface including ability to get& set fields by key (even if you don’t use them, required todeserialize)

I Instances of DBObject can be saved directly to MongoDB.Feeling Fancy? Reflect instead. . .

I Use ReflectionDBObject as a base class for your Beans.I ReflectionDBObject uses reflection to proxy your getters & setters

and behave like a DBObject.I Downside: With Java’s single inheritance you are stuck using this

as your base class.Existing Object Model? ORM-Like Solutions. . .

I Morphia (uses JPA-style annotations)I Daybreak (annotation based)I Mungbean (more Java-ey query syntax & PoJo Mapping)

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 17 / 44

Page 28: Java Development with MongoDB - Mongo NYC 2010

Briefly: Working with DBObjects

Getting data from DBObjects is relatively simple in Java. . .Check if a field (a.k.a. Key) exists with containsField()Get a Set<String> of a DBObject’s field names with keySet()Get a specific field with get(String key). This returns Object so youwill need to cast to an expected value./ / DBObject doc = <some row fetched from MongoDB>S t r i n g username = ( S t r i n g ) doc . get ( " username " )

Call toMap() to get back a Map (String, Object) instead.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44

Page 29: Java Development with MongoDB - Mongo NYC 2010

Briefly: Working with DBObjects

Getting data from DBObjects is relatively simple in Java. . .Check if a field (a.k.a. Key) exists with containsField()Get a Set<String> of a DBObject’s field names with keySet()Get a specific field with get(String key). This returns Object so youwill need to cast to an expected value./ / DBObject doc = <some row fetched from MongoDB>S t r i n g username = ( S t r i n g ) doc . get ( " username " )

Call toMap() to get back a Map (String, Object) instead.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44

Page 30: Java Development with MongoDB - Mongo NYC 2010

Briefly: Working with DBObjects

Getting data from DBObjects is relatively simple in Java. . .Check if a field (a.k.a. Key) exists with containsField()Get a Set<String> of a DBObject’s field names with keySet()Get a specific field with get(String key). This returns Object so youwill need to cast to an expected value./ / DBObject doc = <some row fetched from MongoDB>S t r i n g username = ( S t r i n g ) doc . get ( " username " )

Call toMap() to get back a Map (String, Object) instead.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44

Page 31: Java Development with MongoDB - Mongo NYC 2010

Briefly: Working with DBObjects

Getting data from DBObjects is relatively simple in Java. . .Check if a field (a.k.a. Key) exists with containsField()Get a Set<String> of a DBObject’s field names with keySet()Get a specific field with get(String key). This returns Object so youwill need to cast to an expected value./ / DBObject doc = <some row fetched from MongoDB>S t r i n g username = ( S t r i n g ) doc . get ( " username " )

Call toMap() to get back a Map (String, Object) instead.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44

Page 32: Java Development with MongoDB - Mongo NYC 2010

Briefly: Working with DBObjects

Getting data from DBObjects is relatively simple in Java. . .Check if a field (a.k.a. Key) exists with containsField()Get a Set<String> of a DBObject’s field names with keySet()Get a specific field with get(String key). This returns Object so youwill need to cast to an expected value./ / DBObject doc = <some row fetched from MongoDB>S t r i n g username = ( S t r i n g ) doc . get ( " username " )

Call toMap() to get back a Map (String, Object) instead.

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44

Page 33: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 20 / 44

Page 34: Java Development with MongoDB - Mongo NYC 2010

Inserting DocumentsOne at a time:DBObject doc =

BasicDBOBjectBui lder . s t a r t ( ) .add ( " username " , "bwmcadams" ) .add ( " password " , "MongoNYC" ) .add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) .get ( ) ;

/ / DBCol lect ion c o l lc o l l . i n s e r t ( doc ) ;

Got multiple documents? Call insert() in a loop, or passDBObject[] or List<DBObject>Three ways to store your documents:

I INSERT (insert()) always attempts to add a new row.I SAVE (save()) only attempts to insert unless _id is defined.

Otherwise, it will attempt to update the identified document.I UPDATE (update()) Allows you to pass a query to filter by and the

fields to change. Boolean option "multi" specifies if multipledocuments should be updated. Boolean "upsert" specifies that theobject should be inserted if it doesn’t exist (e.g. query doesn’tmatch).

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 22 / 44

Page 35: Java Development with MongoDB - Mongo NYC 2010

Inserting DocumentsOne at a time:DBObject doc =

BasicDBOBjectBui lder . s t a r t ( ) .add ( " username " , "bwmcadams" ) .add ( " password " , "MongoNYC" ) .add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) .get ( ) ;

/ / DBCol lect ion c o l lc o l l . i n s e r t ( doc ) ;

Got multiple documents? Call insert() in a loop, or passDBObject[] or List<DBObject>Three ways to store your documents:

I INSERT (insert()) always attempts to add a new row.I SAVE (save()) only attempts to insert unless _id is defined.

Otherwise, it will attempt to update the identified document.I UPDATE (update()) Allows you to pass a query to filter by and the

fields to change. Boolean option "multi" specifies if multipledocuments should be updated. Boolean "upsert" specifies that theobject should be inserted if it doesn’t exist (e.g. query doesn’tmatch).

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 22 / 44

Page 36: Java Development with MongoDB - Mongo NYC 2010

Inserting DocumentsOne at a time:DBObject doc =

BasicDBOBjectBui lder . s t a r t ( ) .add ( " username " , "bwmcadams" ) .add ( " password " , "MongoNYC" ) .add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) .get ( ) ;

/ / DBCol lect ion c o l lc o l l . i n s e r t ( doc ) ;

Got multiple documents? Call insert() in a loop, or passDBObject[] or List<DBObject>Three ways to store your documents:

I INSERT (insert()) always attempts to add a new row.I SAVE (save()) only attempts to insert unless _id is defined.

Otherwise, it will attempt to update the identified document.I UPDATE (update()) Allows you to pass a query to filter by and the

fields to change. Boolean option "multi" specifies if multipledocuments should be updated. Boolean "upsert" specifies that theobject should be inserted if it doesn’t exist (e.g. query doesn’tmatch).

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 22 / 44

Page 37: Java Development with MongoDB - Mongo NYC 2010

Inserting DocumentsOne at a time:DBObject doc =

BasicDBOBjectBui lder . s t a r t ( ) .add ( " username " , "bwmcadams" ) .add ( " password " , "MongoNYC" ) .add ( " p resen ta t i on " , " Java Development w i th MongoDB" ) .get ( ) ;

/ / DBCol lect ion c o l lc o l l . i n s e r t ( doc ) ;

Got multiple documents? Call insert() in a loop, or passDBObject[] or List<DBObject>Three ways to store your documents:

I INSERT (insert()) always attempts to add a new row.I SAVE (save()) only attempts to insert unless _id is defined.

Otherwise, it will attempt to update the identified document.I UPDATE (update()) Allows you to pass a query to filter by and the

fields to change. Boolean option "multi" specifies if multipledocuments should be updated. Boolean "upsert" specifies that theobject should be inserted if it doesn’t exist (e.g. query doesn’tmatch).

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 22 / 44

Page 38: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 23 / 44

Page 39: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingBasics

Find a single row with findOne(). Takes the first row returned.Getting a cursor of all documents (find() with no query):DBCursor cur = c o l l . f i n d ( ) ;wh i le ( DBObject doc : cur ) {

System . out . p r i n t l n ( doc ) ;}

Query for a specific value. . .DBObject q = new BasicDBObjectBui lder .

s t a r t ( ) .add ( " username " , "bwmcadams" ) .get ( ) ;

DBObject doc = c o l l . f indOne ( q ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 25 / 44

Page 40: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingBasics

Find a single row with findOne(). Takes the first row returned.Getting a cursor of all documents (find() with no query):DBCursor cur = c o l l . f i n d ( ) ;wh i le ( DBObject doc : cur ) {

System . out . p r i n t l n ( doc ) ;}

Query for a specific value. . .DBObject q = new BasicDBObjectBui lder .

s t a r t ( ) .add ( " username " , "bwmcadams" ) .get ( ) ;

DBObject doc = c o l l . f indOne ( q ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 25 / 44

Page 41: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingBasics

Find a single row with findOne(). Takes the first row returned.Getting a cursor of all documents (find() with no query):DBCursor cur = c o l l . f i n d ( ) ;wh i le ( DBObject doc : cur ) {

System . out . p r i n t l n ( doc ) ;}

Query for a specific value. . .DBObject q = new BasicDBObjectBui lder .

s t a r t ( ) .add ( " username " , "bwmcadams" ) .get ( ) ;

DBObject doc = c o l l . f indOne ( q ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 25 / 44

Page 42: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingBasics

You can pass an optional second DBObject parameter to find()and findOne() which specifies the fields to return.If you have an embedded object (for example, an address object)you can retrieve it with dot notation in the fields list (e.g."address.city" retrieves just the city value)Use limit(), skip() and sort() on DBCursor to adjust your results.These all return a new DBCursor.distinct() can be used (on DBCollection to find all distinct valuesfor a given key; it returns a list:L i s t values = c o l l . d i s t i n c t ( " postedBy " ) ; / / con ta ins a l l d i s t i n c t

values i n " postedBy "/∗ ∗ Or , l i m i t what you ’ re look ing f o r w i th a query ∗ /DBObject q = new BasicDBObject ( " postedBy " ,

new BasicDBObject ( " $ne " , "bwmcadams" )) ;

L i s t values = c o l l . d i s t i n c t ( " postedBy " , q ) ;

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 27 / 44

Page 43: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingQuery Operators

MongoDB is no mere Key-Value store. There are myriad powerfuloperators to enhance your MongoDB queries. . .

Conditional Operators: $gt (>), $lt (<), $gte (>=), $lte (<=)Negative Equality: $ne (!=)Array Operators: $in (SQL "IN" clause. . . takes an array), $nin (Oppositeof "IN"), $all (Requires all values in the array match), $size (Match thesize of an array)Field Defined: $exists (boolean argument)(Great in a schemaless world)Regular Expressions (Language dependent - most drivers support it)Pass Arbitrary Javascript with $where (No OR statements, so useWHERE for complex range filters)

Negate any operator with $not

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 28 / 44

Page 44: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingPutting Operators to Work

Using a query operator requires nested objects. . .All posts since a particular date:DBObject q = new BasicDBObject ( " postDate " ,

new BasicDBObject ( " $gte " , new java . u t i l . Date ( ) )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

Find all posts NOT by me:DBObject q = new BasicDBObject ( " postedBy " ,

new BasicDBObject ( " $ne " , "bwmcadams" )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

No syntactic sugar in Java to make it easier. . .Mungbean provides a more fluid query syntax (but isn’t veryMongo-ey), if you use its PoJo mapping.You might also consider evaluating other JVM languages forquerying....

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 30 / 44

Page 45: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingPutting Operators to Work

Using a query operator requires nested objects. . .All posts since a particular date:DBObject q = new BasicDBObject ( " postDate " ,

new BasicDBObject ( " $gte " , new java . u t i l . Date ( ) )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

Find all posts NOT by me:DBObject q = new BasicDBObject ( " postedBy " ,

new BasicDBObject ( " $ne " , "bwmcadams" )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

No syntactic sugar in Java to make it easier. . .Mungbean provides a more fluid query syntax (but isn’t veryMongo-ey), if you use its PoJo mapping.You might also consider evaluating other JVM languages forquerying....

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 30 / 44

Page 46: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingPutting Operators to Work

Using a query operator requires nested objects. . .All posts since a particular date:DBObject q = new BasicDBObject ( " postDate " ,

new BasicDBObject ( " $gte " , new java . u t i l . Date ( ) )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

Find all posts NOT by me:DBObject q = new BasicDBObject ( " postedBy " ,

new BasicDBObject ( " $ne " , "bwmcadams" )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

No syntactic sugar in Java to make it easier. . .Mungbean provides a more fluid query syntax (but isn’t veryMongo-ey), if you use its PoJo mapping.You might also consider evaluating other JVM languages forquerying....

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 30 / 44

Page 47: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingPutting Operators to Work

Using a query operator requires nested objects. . .All posts since a particular date:DBObject q = new BasicDBObject ( " postDate " ,

new BasicDBObject ( " $gte " , new java . u t i l . Date ( ) )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

Find all posts NOT by me:DBObject q = new BasicDBObject ( " postedBy " ,

new BasicDBObject ( " $ne " , "bwmcadams" )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

No syntactic sugar in Java to make it easier. . .Mungbean provides a more fluid query syntax (but isn’t veryMongo-ey), if you use its PoJo mapping.You might also consider evaluating other JVM languages forquerying....

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 30 / 44

Page 48: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingPutting Operators to Work

Using a query operator requires nested objects. . .All posts since a particular date:DBObject q = new BasicDBObject ( " postDate " ,

new BasicDBObject ( " $gte " , new java . u t i l . Date ( ) )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

Find all posts NOT by me:DBObject q = new BasicDBObject ( " postedBy " ,

new BasicDBObject ( " $ne " , "bwmcadams" )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

No syntactic sugar in Java to make it easier. . .Mungbean provides a more fluid query syntax (but isn’t veryMongo-ey), if you use its PoJo mapping.You might also consider evaluating other JVM languages forquerying....

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 30 / 44

Page 49: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingPutting Operators to Work

Using a query operator requires nested objects. . .All posts since a particular date:DBObject q = new BasicDBObject ( " postDate " ,

new BasicDBObject ( " $gte " , new java . u t i l . Date ( ) )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

Find all posts NOT by me:DBObject q = new BasicDBObject ( " postedBy " ,

new BasicDBObject ( " $ne " , "bwmcadams" )) ;

DBCursor posts = c o l l . f i n d ( q ) ;

No syntactic sugar in Java to make it easier. . .Mungbean provides a more fluid query syntax (but isn’t veryMongo-ey), if you use its PoJo mapping.You might also consider evaluating other JVM languages forquerying....

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 30 / 44

Page 50: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 31 / 44

Page 51: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingAltJVM Languages - Syntactic Sugar

Listing 5: Groovy Sampledef q = [postDate: ["$gte:" new java.util.Date()]

] as BasicDBObjectdef q = [postedBy: ["$ne": "bwmcadams"]

] as BasicDBObject

def posts = coll.find(q)

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 33 / 44

Page 52: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingAltJVM Languages - Syntactic Sugar

Listing 6: Scala Sampleval q = "postDate" $gte new java.util.Date()val q = "postedBy" $ne "bwmcadams"

val posts = coll.find(q)

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 35 / 44

Page 53: Java Development with MongoDB - Mongo NYC 2010

MongoDB QueryingAltJVM Languages - Syntactic Sugar

Listing 7: Jython Sampleq = {"postDate": {"$gte": datetime.datetime.now()}}q = {"postedBy": {"$ne": "bwmcadams"}}

posts = coll.find(q)

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 37 / 44

Page 54: Java Development with MongoDB - Mongo NYC 2010

Outline

1 MongoDB + Java BasicsSetting up your Java environmentConnecting to MongoDB

2 Working with MongoDBCollections + DocumentsInserting Documents to MongoDBQuerying MongoDBAltJVM Languages - Syntactic SugarIndexes, etc

3 Final Remarks

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 38 / 44

Page 55: Java Development with MongoDB - Mongo NYC 2010

Indexes

MongoDB Indexes work much like RDBMS indexes. . .

Create a single-key index:DBObject i dx = new BasicDBObject ( " postedBy " , 1) ;c o l l . c reate Index ( idx ) ;

This defaults to ascending order (-1 = descending), with a systemgenerated name.

For a multi-key/compound-key index:

DBObject i dx = new BasicDBObjectBui lder . s t a r t ( ) .add ( " postedBy " , 1) .add ( " postDate " , −1) .get ( ) ;

c o l l . c reate Index ( idx ) ;

This sorts postedBy ascending, but postDate descending.

If you want to index an embedded field, simply use the query dotnotation (e.g. "address.city")

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 40 / 44

Page 56: Java Development with MongoDB - Mongo NYC 2010

IndexesOptions

MongoDB Indexes work much like RDBMS indexes. . .

You can pass a second DBObject of options to change the type of index,name, etc:

DBObject i dx = new BasicDBObject ( " s lug " , 1) ;DBObject opts = new BasicDBObject ( " unique " : t r ue ) ;c o l l . c reate Index ( idx , opts ) ;

This will ensure that the "slug" field is unique across all entries. (A list ofcomplete options is available in the MongoDB Documentation)

Consider using ensureIndex() instead of createIndex().

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 42 / 44

Page 57: Java Development with MongoDB - Mongo NYC 2010

Things We Didn’t Cover

Things we didn’t cover, but you should spend some time exploring. . .Map/Reduce (great for more complex aggregation)Geospatial indexes and queriesAggregation queries such as Grouping statements (Which useJavaScript functions)GridFS (Efficient storage of large files)Lots more at MongoDB.org . . .

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 43 / 44

Page 58: Java Development with MongoDB - Mongo NYC 2010

Contact Info + Where To Learn MoreContact Me

I twitter: @ritI email: [email protected]

Pressing Questions?I IRC - freenode.net #mongodbI MongoDB Users List -http://groups.google.com/group/mongodb-user

Mongo Java Language Center - http://mongodb.org/display/DOCS/Java+Language+Center(Links to Java driver docs, and many of the third party libraries)

Morphia - http://code.google.com/p/morphia/Mungbean -http://github.com/jannehietamaki/mungbean

Experimental JDBC Driver -http://github.com/erh/mongo-jdbc

Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 44 / 44