SlideShare a Scribd company logo
FIRST STEPS WITH 
MONGO 
AND JAVA 
JUSTIN LEE 
MEMBER OF TECHNICAL STAFF 
@ MONGODB.COM 
http://antwerkz.com @evanchooly
WHAT IS MONGO? 
Name derives from "humongous" which mean big 
It is a scalable, high-performance, open source NoSQL database 
Document oriented 
Fully indexable 
Replication and HA 
Map Reduce and Aggregation framework 
GridFS
DOCUMENT ORIENTED 
In an RDBMS, one entity is typically mapped across multiple 
cells in one row of a table 
sometimes multiple objects with embedded entities 
Documents stored in json-style documents 
http://bsonspec.org 
actually BSON ( ) 
BSON adds some "extra" information to documents, like 
length prefixes, that make traversal efficient.
WHAT DOES A DOCUMENT LOOK LIKE? 
db.users.find().pretty() 
{ "_id" : ObjectId("50fdb55a18c650918ee414be"), 
"className" : "com.antwerkz.mongo.model.User", 
"firstName" : "Jules", 
"lastName" : "Winnfield", 
"email" : "jules@hotmail.com", 
"addresses" : [ 
{ 
"street" : "1858 N Vermont Ave", 
"city" : "Los Angeles", 
"state" : "CA", 
"zip" : "90027" 
} 
]}
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
COMPARISONS 
JAVA DRIVER 
MORPHIA
GETTING STARTED 
https://www.mongodb.org/downloads 
<dependency> 
<groupId>org.mongodb</groupId> 
<artifactId>mongo-java-driver</artifactId> 
<version>2.12.3</version> 
</dependency> 
<dependency> 
<groupId>org.mongodb.morphia</groupId> 
<artifactId>morphia</artifactId> 
<version>0.108</version> 
</dependency>
QUERIES 
db.product_orders.find({ fulfilled : true, total : { $gte : 5000.0 } 
}).sort({total : 1}) 
db.product_orders.find({ $or : [ { size : { $lte : 3 } }, { fulfilled : false 
} ] })
db.product_orders.find({ fulfilled : true, total : { $gte : 5000.0 } 
}).sort({total : 1}) 
BasicDBObject query = new BasicDBObject("fulfilled", true) 
.append("total", new BasicDBObject("$gte", 5000.0)); 
DBCursor cursor = db.getCollection(PRODUCT_ORDERS).find(query) 
.sort(new BasicDBObject("total", 1)); 
while (cursor.hasNext()) { 
orders.add(new ProductOrder(cursor.next())); 
} 
ds.createQuery(ProductOrder.class) 
.field("fulfilled").equal(true) 
.filter("total >=", 5000.0) 
// .field("total").greaterThanOrEq(5000.0) <-- can be done either way 
.order("total").asList() 
= == > >= < <= != <> mod 
in nin all exists elem size within near
db.product_orders.find({ $or : [ { size : { $lte : 3 } }, { fulfilled : false } ] 
}) 
BasicDBList list = new BasicDBList(); 
list.add(new BasicDBObject("fulfilled", false)); 
list.add(new BasicDBObject("size", new BasicDBObject("$lte", 3))); 
DBCursor cursor = db.getCollection("product_orders") 
.find(new BasicDBObject("$or", list)); 
while (cursor.hasNext()) { 
orders.add(new ProductOrder(cursor.next())); 
} 
Query<ProductOrder> query = ds.createQuery(ProductOrder.class); 
query.or( 
query.criteria("fulfilled").equal(false), 
query.criteria("size").lessThanOrEq(3) 
); 
return query.asList();
UPDATES 
db.product_orders.update({ size : 3 }, { $set : { total : 400 } } ) 
db.product_orders.update({}, { $push : { baubles : { color : "red" } } 
} )
db.product_orders.update({ size : 3 }, { $set : { total : 400 } } ) 
BasicDBObject query = new BasicDBObject("size", 3); 
BasicDBObject update = new BasicDBObject("$set", 
new BasicDBObject("total", 400)); 
DBCollection collection = db.getCollection("product_orders"); 
collection.update(query, update/*, true/false, true/false*/); 
Query<ProductOrder> query = ds.createQuery(ProductOrder.class) 
.filter("size", 3); 
UpdateOperations<ProductOrder> update 
= ds.createUpdateOperations(ProductOrder.class) 
.set("total", 400); 
ds.update(query, update/* true/false for upsert*/); 
// ds.updateFirst(query, update/* true/false for upsert*/);
db.product_orders.update({}, { $push : { baubles : { color : "red" } } }, 
false, true ) 
BasicDBObject update = new BasicDBObject("$push", 
new BasicDBObject("baubles", new BasicDBObject("color", "red"))); 
db.getCollection(PRODUCT_ORDERS).update(new BasicDBObject(), update, 
false, true); 
UpdateOperations<ProductOrder> update = 
ds.createUpdateOperations(ProductOrder.class) 
.disableValidation() 
.add("baubles", new BasicDBObject("color", "red"), true); 
ds.update(ds.createQuery(ProductOrder.class), update, false);
CODE SAMPLES
LINKS 
http://www.mongodb.org/ 
https://github.com/mongodb/morphia 
https://github.com/evanchooly/mongo-java
FIRST STEPS WITH 
MONGO 
AND JAVA 
JUSTIN LEE 
MEMBER OF TECHNICAL STAFF 
@ MONGODB.COM 
http://antwerkz.com @evanchooly

More Related Content

PPTX
PostgreSQL's Secret NoSQL Superpowers
ODP
2011 Mongo FR - Indexing in MongoDB
PDF
Mongo Presentation by Metatagg Solutions
PPT
PhpstudyTokyo MongoDB PHP CakePHP
PDF
Embedding a language into string interpolator
TXT
Getting Started With MongoDB
PDF
Php 2
PDF
An introduction to MongoDB
PostgreSQL's Secret NoSQL Superpowers
2011 Mongo FR - Indexing in MongoDB
Mongo Presentation by Metatagg Solutions
PhpstudyTokyo MongoDB PHP CakePHP
Embedding a language into string interpolator
Getting Started With MongoDB
Php 2
An introduction to MongoDB

What's hot (20)

KEY
Introduction to MongoDB
PPTX
Aggregation in MongoDB
PPTX
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
PPT
jQuery Datatables With MongDb
TXT
Threading
PDF
Mongo db for C# Developers
PPTX
Introduction to the new official C# Driver developed by 10gen
PDF
Mongo db for c# developers
PPTX
"Powerful Analysis with the Aggregation Pipeline (Tutorial)"
PPT
Persistencia de datos con Parse
PPTX
NoSQL with MongoDB
DOCX
Format xls sheets Demo Mode
PDF
MongoDB Aggregation Framework
PDF
与 PHP 和 Perl 使用 MySQL 数据库
PPTX
Webinar: Exploring the Aggregation Framework
PDF
Book integrated assignment
PDF
DBIx::Class walkthrough @ bangalore pm
PPTX
Mongo DB 102
PPTX
1403 app dev series - session 5 - analytics
DOC
File System Operations
Introduction to MongoDB
Aggregation in MongoDB
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
jQuery Datatables With MongDb
Threading
Mongo db for C# Developers
Introduction to the new official C# Driver developed by 10gen
Mongo db for c# developers
"Powerful Analysis with the Aggregation Pipeline (Tutorial)"
Persistencia de datos con Parse
NoSQL with MongoDB
Format xls sheets Demo Mode
MongoDB Aggregation Framework
与 PHP 和 Perl 使用 MySQL 数据库
Webinar: Exploring the Aggregation Framework
Book integrated assignment
DBIx::Class walkthrough @ bangalore pm
Mongo DB 102
1403 app dev series - session 5 - analytics
File System Operations
Ad

Viewers also liked (7)

PDF
Jongo mongo sv
PPTX
Mongo db
KEY
OSCON 2012 MongoDB Tutorial
PDF
Intro To MongoDB
PPT
Introduction to MongoDB
PPTX
Back to Basics: My First MongoDB Application
PPTX
MongoDB Auto-Sharding at Mongo Seattle
Jongo mongo sv
Mongo db
OSCON 2012 MongoDB Tutorial
Intro To MongoDB
Introduction to MongoDB
Back to Basics: My First MongoDB Application
MongoDB Auto-Sharding at Mongo Seattle
Ad

Similar to Dev Jumpstart: Build Your First App with MongoDB (20)

PDF
PDF
Spray Json and MongoDB Queries: Insights and Simple Tricks.
PDF
Building Apps with MongoDB
PDF
Using web2py's DAL in other projects or frameworks
PPTX
Introduction tomongodb
PPTX
Querying mongo db
PDF
Building DSLs with Groovy
PPTX
MongoDB - Features and Operations
PDF
Latinoware
PPTX
MongoDb Database Notes Presentation Latest
PPTX
Introduction to NOSQL And MongoDB
PDF
Data access 2.0? Please welcome: Spring Data!
PDF
Superficial mongo db
PPTX
Introduction to MongoDB
PDF
An introduction into Spring Data
PDF
Dealing with Azure Cosmos DB
PPTX
MongoDB
KEY
CouchDB on Android
PDF
Building Your First MongoDB App
PPTX
introduction to Mongodb
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Building Apps with MongoDB
Using web2py's DAL in other projects or frameworks
Introduction tomongodb
Querying mongo db
Building DSLs with Groovy
MongoDB - Features and Operations
Latinoware
MongoDb Database Notes Presentation Latest
Introduction to NOSQL And MongoDB
Data access 2.0? Please welcome: Spring Data!
Superficial mongo db
Introduction to MongoDB
An introduction into Spring Data
Dealing with Azure Cosmos DB
MongoDB
CouchDB on Android
Building Your First MongoDB App
introduction to Mongodb

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...

Recently uploaded (20)

PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPT
Teaching material agriculture food technology
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Programs and apps: productivity, graphics, security and other tools
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
sap open course for s4hana steps from ECC to s4
Big Data Technologies - Introduction.pptx
Machine Learning_overview_presentation.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
Spectroscopy.pptx food analysis technology
Teaching material agriculture food technology
A comparative analysis of optical character recognition models for extracting...
Review of recent advances in non-invasive hemoglobin estimation
Programs and apps: productivity, graphics, security and other tools

Dev Jumpstart: Build Your First App with MongoDB

  • 1. FIRST STEPS WITH MONGO AND JAVA JUSTIN LEE MEMBER OF TECHNICAL STAFF @ MONGODB.COM http://antwerkz.com @evanchooly
  • 2. WHAT IS MONGO? Name derives from "humongous" which mean big It is a scalable, high-performance, open source NoSQL database Document oriented Fully indexable Replication and HA Map Reduce and Aggregation framework GridFS
  • 3. DOCUMENT ORIENTED In an RDBMS, one entity is typically mapped across multiple cells in one row of a table sometimes multiple objects with embedded entities Documents stored in json-style documents http://bsonspec.org actually BSON ( ) BSON adds some "extra" information to documents, like length prefixes, that make traversal efficient.
  • 4. WHAT DOES A DOCUMENT LOOK LIKE? db.users.find().pretty() { "_id" : ObjectId("50fdb55a18c650918ee414be"), "className" : "com.antwerkz.mongo.model.User", "firstName" : "Jules", "lastName" : "Winnfield", "email" : "jules@hotmail.com", "addresses" : [ { "street" : "1858 N Vermont Ave", "city" : "Los Angeles", "state" : "CA", "zip" : "90027" } ]}
  • 9. GETTING STARTED https://www.mongodb.org/downloads <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>org.mongodb.morphia</groupId> <artifactId>morphia</artifactId> <version>0.108</version> </dependency>
  • 10. QUERIES db.product_orders.find({ fulfilled : true, total : { $gte : 5000.0 } }).sort({total : 1}) db.product_orders.find({ $or : [ { size : { $lte : 3 } }, { fulfilled : false } ] })
  • 11. db.product_orders.find({ fulfilled : true, total : { $gte : 5000.0 } }).sort({total : 1}) BasicDBObject query = new BasicDBObject("fulfilled", true) .append("total", new BasicDBObject("$gte", 5000.0)); DBCursor cursor = db.getCollection(PRODUCT_ORDERS).find(query) .sort(new BasicDBObject("total", 1)); while (cursor.hasNext()) { orders.add(new ProductOrder(cursor.next())); } ds.createQuery(ProductOrder.class) .field("fulfilled").equal(true) .filter("total >=", 5000.0) // .field("total").greaterThanOrEq(5000.0) <-- can be done either way .order("total").asList() = == > >= < <= != <> mod in nin all exists elem size within near
  • 12. db.product_orders.find({ $or : [ { size : { $lte : 3 } }, { fulfilled : false } ] }) BasicDBList list = new BasicDBList(); list.add(new BasicDBObject("fulfilled", false)); list.add(new BasicDBObject("size", new BasicDBObject("$lte", 3))); DBCursor cursor = db.getCollection("product_orders") .find(new BasicDBObject("$or", list)); while (cursor.hasNext()) { orders.add(new ProductOrder(cursor.next())); } Query<ProductOrder> query = ds.createQuery(ProductOrder.class); query.or( query.criteria("fulfilled").equal(false), query.criteria("size").lessThanOrEq(3) ); return query.asList();
  • 13. UPDATES db.product_orders.update({ size : 3 }, { $set : { total : 400 } } ) db.product_orders.update({}, { $push : { baubles : { color : "red" } } } )
  • 14. db.product_orders.update({ size : 3 }, { $set : { total : 400 } } ) BasicDBObject query = new BasicDBObject("size", 3); BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("total", 400)); DBCollection collection = db.getCollection("product_orders"); collection.update(query, update/*, true/false, true/false*/); Query<ProductOrder> query = ds.createQuery(ProductOrder.class) .filter("size", 3); UpdateOperations<ProductOrder> update = ds.createUpdateOperations(ProductOrder.class) .set("total", 400); ds.update(query, update/* true/false for upsert*/); // ds.updateFirst(query, update/* true/false for upsert*/);
  • 15. db.product_orders.update({}, { $push : { baubles : { color : "red" } } }, false, true ) BasicDBObject update = new BasicDBObject("$push", new BasicDBObject("baubles", new BasicDBObject("color", "red"))); db.getCollection(PRODUCT_ORDERS).update(new BasicDBObject(), update, false, true); UpdateOperations<ProductOrder> update = ds.createUpdateOperations(ProductOrder.class) .disableValidation() .add("baubles", new BasicDBObject("color", "red"), true); ds.update(ds.createQuery(ProductOrder.class), update, false);
  • 18. FIRST STEPS WITH MONGO AND JAVA JUSTIN LEE MEMBER OF TECHNICAL STAFF @ MONGODB.COM http://antwerkz.com @evanchooly