Dev Jumpstart: Build Your First App with MongoDB

Post on 05-Dec-2014

200 views 0 download

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.

Transcript of Dev Jumpstart: Build Your First App with MongoDB

http://antwerkz.com @evanchooly

FIRST STEPS WITHMONGO

AND JAVAJUSTIN LEE

MEMBER OF TECHNICAL STAFF@ MONGODB.COM

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

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

db.users.find().pretty()

WHAT DOES A DOCUMENT LOOK LIKE?

{"_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" }]}

COMPARISONSJAVA DRIVER

MORPHIA

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>

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

= == > >= < <= != <> modin 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();

UPDATESdb.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

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

http://antwerkz.com @evanchooly

FIRST STEPS WITHMONGO

AND JAVAJUSTIN LEE

MEMBER OF TECHNICAL STAFF@ MONGODB.COM