Sponsored by Hortis

Jongo

Querying MongoDB from Java without a fuss

Ivan Hristov

Software Engineer at Hortis
Geneva MongoDB User Group {@genevamug, http://genevamug.ch}
@iv_hristov
Blog: http://ingini.org
Resources
https://github.com/ihr/
  • jongo-by-example
  • morphia-by-example
  • spring-data-by-example
Roadmap
Query MongoDB from Java as if using MongoDB shell
  • CRUD
  • Aggregation Framework
  • Geospatial queries
  • Jongo vs. Morphia vs. Spring Data
MongoDB ... and the documents
database collection document
                
{
  "_id": ObjectId("524048b68faa3a7de509a5bb"),
  "name" : "Ivan",
  "twitter": "@iv_hristov",
  "projects": [
    {"name": "FEST-Reflect", "description": "Easy Java reflection"},
    {"name": "Jongo", "site" : "http://jongo.org/",
      "creators": [
        {"name": "Benoit Guérout", "twitter": "@bguerout"},
        {"name": "Yves Amsellem", "twitter": "@amsellemyves"}
      ]}
  ]
}
                
            
Create
MongoDB Shell
                
db.characters.insert({first_name: 'Eddard', last_name: 'Stark'});
                
            
Jongo
                
characters.insert("{first_name: 'Eddard', last_name: 'Stark'}");
                
            
Read
MongoDB Shell
                
db.characters.findOne({_id : ObjectId("525ae7e130049e2a5c27ee39")});
                
            
Jongo
                
characters.findOne(new ObjectId("525ae7e130049e2a5c27ee39")).as(...);
                
            
Read
MongoDB Shell
                
db.characters.findOne({gender: 'FEMALE', first_name: 'Arya'});
                
            
Jongo
                
characters.findOne("{gender: #, first_name : #}", FEMALE, "Arya").as(...)
                
            
Update
MongoDB Shell
                
db.weapons.update({_id: 'Lightbringer'}, {$set : {details: '...'}});
                
            
Jongo
                
weapons.update("{_id: 'Lightbringer').with("{$set: {details: '...'}}");
                
            
Delete
MongoDB Shell
                
db.weapons.remove({_id: 'Lightbringer'});
                
            
Jongo
                
weapons.remove("{_id: 'Lightbringer'}");
                
            
Aggregation Pipeline
Geospatial functionality
Jongo vs. Morphia vs. Spring
Jongo
                
characters.findOne("{gender : #, first_name: #}", FEMALE, "Arya")
.as(Heroine.class);
                
            
Morphia
                
ds.find(Heroine.class, "gender", Gender.FEMALE).field("first_name")
.equal("Arya");
                
            
Spring data
                
mongoTemplate.findOne(Query.query(Criteria.where("gender").is(Gender.FEMALE)
.and("first_name").is("Arya")), Heroine.class);
                
            
Jongo vs. Morphia vs. Spring
Jongo
                
weapons.update("{_id:'Lightbringer'}").with("{$set: {details: #}}",details);
                
            
Morphia
                
ds.update(ds.createQuery(Weapon.class).field("id").equal("Lightbringer"),
ds.createUpdateOperations(Weapon.class).set("details", details));
                
            
Spring data
                
mongoTemplate.findAndModify(Query.query(Criteria.where("_id").is("Lightbringer")),
new Update().set("details", details), Weapon.class);
                
            
GenevaMUG

Justin Lee
@evanchooly
http://antwerkz.com

MongoDB for JPA Developers

November, 19th 2013 @ 18h30

http://genevamug.ch

@genevamug

Q & A



http://jongo.org/
@bguerout
@amsellemyves
@iv_hristov
http://ingini.org/