18
http://antwerkz.com @evanchooly FIRST STEPS WITH MONGO AND JAVA JUSTIN LEE MEMBER OF TECHNICAL STAFF @ MONGODB.COM

Dev Jumpstart: Build Your First App with MongoDB

  • Upload
    mongodb

  • View
    203

  • Download
    0

Embed Size (px)

DESCRIPTION

New to MongoDB? This talk will introduce the philosophy and features of MongoDB. We’ll discuss the benefits of the document-based data model that MongoDB offers by walking through how one can build a simple app to store books. We’ll cover inserting, updating, and querying the database of books. This session will jumpstart your knowledge of MongoDB development, providing you with context for the rest of the day's content.

Citation preview

Page 1: Dev Jumpstart: Build Your First App with MongoDB

http://antwerkz.com @evanchooly

FIRST STEPS WITHMONGO

AND JAVAJUSTIN LEE

MEMBER OF TECHNICAL STAFF@ MONGODB.COM

Page 2: Dev Jumpstart: Build Your First App with MongoDB

WHAT IS MONGO?Name derives from "humongous" which mean bigIt is a scalable, high-performance, open source NoSQL databaseDocument orientedFully indexableReplication and HAMap Reduce and Aggregation frameworkGridFS

Page 3: Dev Jumpstart: Build Your First App with MongoDB

DOCUMENT ORIENTEDIn an RDBMS, one entity is typically mapped across multiplecells in one row of a table

sometimes multiple objects with embedded entitiesDocuments stored in json-style documents

actually BSON ( )BSON adds some "extra" information to documents, likelength prefixes, that make traversal efficient.

http://bsonspec.org

Page 4: Dev Jumpstart: Build Your First App with MongoDB

db.users.find().pretty()

WHAT DOES A DOCUMENT LOOK LIKE?

{"_id" : ObjectId("50fdb55a18c650918ee414be"),"className" : "com.antwerkz.mongo.model.User","firstName" : "Jules","lastName" : "Winnfield","email" : "[email protected]","addresses" : [ { "street" : "1858 N Vermont Ave", "city" : "Los Angeles", "state" : "CA", "zip" : "90027" }]}

Page 5: Dev Jumpstart: Build Your First App with MongoDB
Page 6: Dev Jumpstart: Build Your First App with MongoDB
Page 7: Dev Jumpstart: Build Your First App with MongoDB
Page 8: Dev Jumpstart: Build Your First App with MongoDB

COMPARISONSJAVA DRIVER

MORPHIA

Page 9: Dev Jumpstart: Build Your First App with MongoDB

https://www.mongodb.org/downloads

GETTING STARTED<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>

Page 10: Dev Jumpstart: Build Your First App with MongoDB

QUERIESdb.product_orders.find({ fulfilled : true, total : { $gte : 5000.0 }}).sort({total : 1})db.product_orders.find({ $or : [ { size : { $lte : 3 } }, { fulfilled : false} ] })

Page 11: Dev Jumpstart: Build Your First App with MongoDB

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()

= == > >= < <= != <> modin nin all exists elem size within near

Page 12: Dev Jumpstart: Build Your First App with MongoDB

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();

Page 13: Dev Jumpstart: Build Your First App with MongoDB

UPDATESdb.product_orders.update({ size : 3 }, { $set : { total : 400 } } )db.product_orders.update({}, { $push : { baubles : { color : "red" } }} )

Page 14: Dev Jumpstart: Build Your First App with MongoDB

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*/);

Page 15: Dev Jumpstart: Build Your First App with MongoDB

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);

Page 16: Dev Jumpstart: Build Your First App with MongoDB

CODE SAMPLES

Page 17: Dev Jumpstart: Build Your First App with MongoDB

LINKShttp://www.mongodb.org/https://github.com/mongodb/morphiahttps://github.com/evanchooly/mongo-java

Page 18: Dev Jumpstart: Build Your First App with MongoDB

http://antwerkz.com @evanchooly

FIRST STEPS WITHMONGO

AND JAVAJUSTIN LEE

MEMBER OF TECHNICAL STAFF@ MONGODB.COM