Professional Documents
Culture Documents
Brendan W. McAdams
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 1 / 44
Outline
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 2 / 44
Outline
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 3 / 44
Adding the MongoDB Driver To Your Project
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 5 / 44
Adding the MongoDB Driver To Your Project
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 5 / 44
Outline
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 6 / 44
Simple Connection
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 8 / 44
Simple Connection
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 8 / 44
Simple Connection
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 8 / 44
Outline
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 9 / 44
Working with Collections
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 11 / 44
Working with Collections
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 11 / 44
MongoDB Documents
BSON
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 12 / 44
MongoDB Documents
From Java: BasicDBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44
MongoDB Documents
From Java: BasicDBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44
MongoDB Documents
From Java: BasicDBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44
MongoDB Documents
From Java: BasicDBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44
MongoDB Documents
From Java: BasicDBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44
MongoDB Documents
From Java: BasicDBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44
MongoDB Documents
From Java: BasicDBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 14 / 44
MongoDB Documents
From Java: BasicDBObjectBuilder
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44
MongoDB Documents
From Java: BasicDBObjectBuilder
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44
MongoDB Documents
From Java: BasicDBObjectBuilder
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44
MongoDB Documents
From Java: BasicDBObjectBuilder
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 16 / 44
MongoDB Documents
Implementation by Extension: DBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 17 / 44
MongoDB Documents
Implementation by Extension: DBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 17 / 44
MongoDB Documents
Implementation by Extension: DBObject
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 17 / 44
Briefly: Working with DBObjects
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44
Briefly: Working with DBObjects
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44
Briefly: Working with DBObjects
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44
Briefly: Working with DBObjects
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44
Briefly: Working with DBObjects
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 19 / 44
Outline
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 20 / 44
Inserting Documents
One at a time:
DBObject doc =
BasicDBOBjectBuilder . s t a r t ( ) .
add ( " username " , " bwmcadams " ) .
add ( " password " , "MongoNYC" ) .
add ( " p r e s e n t a t i o n " , " Java Development w i t h MongoDB" ) .
get ( ) ;
/ / DBCollection c o l l
c o l l . i n s e r t ( doc ) ;
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 23 / 44
MongoDB Querying
Basics
Find a single row with findOne(). Takes the first row returned.
Getting a cursor of all documents (find() with no query):
DBCursor c u r = c o l l . f i n d ( ) ;
w h i l e ( DBObject doc : c u r ) {
System . o u t . p r i n t l n ( doc ) ;
}
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 25 / 44
MongoDB Querying
Basics
Find a single row with findOne(). Takes the first row returned.
Getting a cursor of all documents (find() with no query):
DBCursor c u r = c o l l . f i n d ( ) ;
w h i l e ( DBObject doc : c u r ) {
System . o u t . p r i n t l n ( doc ) ;
}
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 25 / 44
MongoDB Querying
Basics
Find a single row with findOne(). Takes the first row returned.
Getting a cursor of all documents (find() with no query):
DBCursor c u r = c o l l . f i n d ( ) ;
w h i l e ( DBObject doc : c u r ) {
System . o u t . p r i n t l n ( doc ) ;
}
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 25 / 44
MongoDB Querying
Basics
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 27 / 44
MongoDB Querying
Query Operators
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 28 / 44
MongoDB Querying
Putting Operators to Work
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 31 / 44
MongoDB Querying
AltJVM Languages - Syntactic Sugar
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 33 / 44
MongoDB Querying
AltJVM Languages - Syntactic Sugar
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 35 / 44
MongoDB Querying
AltJVM Languages - Syntactic Sugar
posts = coll.find(q)
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 37 / 44
Outline
3 Final Remarks
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 38 / 44
Indexes
MongoDB Indexes work much like RDBMS indexes. . .
Create a single-key index:
DBObject i d x = new BasicDBObject ( " postedBy " , 1 ) ;
c o l l . createIndex ( idx ) ;
Brendan W. McAdams (Novus Partners, Inc.) Java Development with MongoDB Mongo NYC, May 2010 40 / 44
Indexes
Options
This will ensure that the "slug" field is unique across all entries. (A list of
complete 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
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 queries
Aggregation queries such as Grouping statements (Which use
JavaScript 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
Contact Info + Where To Learn More
Contact Me
I twitter: @rit
I email: bwmcadams@gmail.com
Pressing Questions?
I IRC - freenode.net #mongodb
I 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