Slide 1

Slide 1 text

Technical Evangelist, MongoDB
 @tgrall Tugdual Grall @EspritJUG Building your first app;
 an introduction to MongoDB

Slide 2

Slide 2 text

@tgrall [email protected] MongoDB@ESPRITJUG2014 • Day 1 : Introduction • Discover MongoDB • Day 2 : Deep Dive into Queries and Analytics • Advanced CRUD operations • Aggregation, GeoSpatial, Full Text • Day 2 : Fun with MongoDB and Javascript • WebSockets • Node.js, AngularJS

Slide 3

Slide 3 text

@tgrall [email protected] { “about” : “me” } Tugdual “Tug” Grall • MongoDB • Technical Evangelist • Couchbase • Technical Evangelist • eXo • CTO • Oracle • Developer/Product Manager • Mainly Java/SOA • Developer in consulting firms • Web • @tgrall • http://blog.grallandco.com • tgrall • NantesJUG co-founder • Pet Project : • http://www.resultri.com • [email protected][email protected]

Slide 4

Slide 4 text

What is MongoDB?

Slide 5

Slide 5 text

@tgrall [email protected] MongoDB is a ___________ database • Document • Open source • High performance • Horizontally scalable • Full featured

Slide 6

Slide 6 text

@tgrall [email protected] Document Database • Not for .PDF & .DOC files • A document is essentially an associative array • Document = JSON object • Document = PHP Array • Document = Python Dict • Document = Ruby Hash • etc

Slide 7

Slide 7 text

@tgrall [email protected] Document Database Relational MongoDB { first_name: ‘Paul’, surname: ‘Miller’ city: ‘London’, location: [45.123,47.232], cars: [ { model: ‘Bently’, year: 1973, value: 100000}, { model: ‘Rolls Royce’, year: 1965,! value: 330000}! } }

Slide 8

Slide 8 text

@tgrall [email protected] Open Source • MongoDB is an open source project • On GitHub • Licensed under the AGPL • Started & sponsored by 10gen • Commercial licenses available • Contributions welcome

Slide 9

Slide 9 text

@tgrall [email protected] High Performance • Written in C++ • Extensive use of memory-mapped files 
 i.e. read-through write-through memory caching. • Runs nearly everywhere • Data serialized as BSON (fast parsing) • Full support for primary & secondary indexes • Document model = less work

Slide 10

Slide 10 text

Slide 11

Slide 11 text

@tgrall [email protected] Database Landscape

Slide 12

Slide 12 text

@tgrall [email protected] Full Featured • Ad Hoc queries • Real time aggregation • Rich query capabilities • Strongly consistent • Geospatial features • Support for most programming languages • Flexible schema

Slide 13

Slide 13 text

@tgrall [email protected] Full Featured Queries • Find Paul’s cars • Find everybody in London with a car built between 1970 and 1980 Geospatial • Find all of the car owners within 5km of Trafalgar Sq. Aggregation • Calculate the average value of Paul’s car collection Map Reduce • What is the ownership pattern of colors by geography over time? (is purple trending up in China?) { first_name: ‘Paul’, surname: ‘Miller’, city: ‘London’, location: { ! type: “Point”, ! coordinates : ! ! [-0.128, 51.507] ! },! cars: [ { model: ‘Bentley’, year: 1973, value: 100000, … }, { model: ‘Rolls Royce’, year: 1965, value: 330000, … } } } Text Search • Find all the cars described as having leather seats

Slide 14

Slide 14 text

@tgrall [email protected] mongodb.org/downloads

Slide 15

Slide 15 text

$ tar –z xvf mongodb-osx-x86_64-2.6.x.tgz! $ cd mongodb-osx-i386-2.6.0/bin! $ mkdir –p /data/db! $ ./mongod Running MongoDB

Slide 16

Slide 16 text

MacBook-Air-:~ $ mongo! MongoDB shell version: 2.6.0! connecting to: test! > db.test.insert({text: 'Welcome to MongoDB'})! > db.test.find().pretty()! {! ! "_id" : ObjectId("51c34130fbd5d7261b4cdb55"),! ! "text" : "Welcome to MongoDB"! } Mongo Shell

Slide 17

Slide 17 text

Document Database

Slide 18

Slide 18 text

@tgrall [email protected] Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard

Slide 19

Slide 19 text

@tgrall [email protected] Let’s Build a Blog

Slide 20

Slide 20 text

@tgrall [email protected] First step in any application is Determine your entities

Slide 21

Slide 21 text

@tgrall [email protected] Entities in our Blogging System • Users (post authors) • Article • Comments • Tags

Slide 22

Slide 22 text

@tgrall [email protected] In a relational base app We would start by doing schema design

Slide 23

Slide 23 text

@tgrall [email protected] Typical (relational) ERD

Slide 24

Slide 24 text

@tgrall [email protected] In a MongoDB based app We start building our app and let the schema evolve

Slide 25

Slide 25 text

@tgrall [email protected] MongoDB ERD

Slide 26

Slide 26 text

Working With MongoDB

Slide 27

Slide 27 text

@tgrall [email protected] Demo time !

Slide 28

Slide 28 text

var user = { ! ! ! ! ! username: ’tgrall', ! ! ! ! ! first_name: ’Tugdual',! ! ! ! ! last_name: ’Grall',! } Start with an object 
 (or array, hash, dict, etc)

Slide 29

Slide 29 text

>db! test! > use blog! switching to db blog ! ! > db.users.insert( user ) Switch to Your DB

Slide 30

Slide 30 text

> db.users.insert(user) Insert the Record No collection creation necessary

Slide 31

Slide 31 text

> db.users.findOne()! {! ! "_id" : ObjectId("50804d0bd94ccab2da652599"),! ! "username" : ”tgrall",! ! "first_name" : ”Tugdual",! ! "last_name" : ”Grall"! } Find One Record

Slide 32

Slide 32 text

@tgrall [email protected] _id • _id is the primary key in MongoDB • Automatically indexed • Automatically created as an ObjectId if not provided • Any unique immutable value could be used

Slide 33

Slide 33 text

@tgrall [email protected] ObjectId • ObjectId is a special 12 byte value • Guaranteed to be unique across your cluster • ObjectId("50804d0bd94ccab2da652599")
 
 |----ts-----||---mac---||-pid-||----inc-----|
 4 3 2 3

Slide 34

Slide 34 text

> db.article.insert({ ! ! ! ! ! ! title: ‘Hello World’,! ! ! ! ! ! body: ‘This is my first blog post’,! ! ! ! ! ! date: new Date(‘2013-06-20’),! ! ! ! ! ! username: ‘tgrall’,! ! ! ! ! ! tags: [‘adventure’, ‘mongodb’],! ! ! ! ! ! comments: [ ]! }) Creating a Blog Post

Slide 35

Slide 35 text

> db.article.find().pretty()! {! ! "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"),! ! "title" : "Hello World",! ! "body" : "This is my first blog post",! ! "date" : ISODate("2013-06-20T00:00:00Z"),! ! "username" : "tgrall",! ! "tags" : [! ! ! "adventure",! ! ! "mongodb"! ! ],! ! "comments" : [ ]! } Finding the Post

Slide 36

Slide 36 text

> db.article.find({tags:'adventure'}).pretty()! {! ! "_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"),! ! "title" : "Hello World",! ! "body" : "This is my first blog post",! ! "date" : ISODate("2013-06-20T00:00:00Z"),! ! "username" : "tgrall",! ! "tags" : [! ! ! "adventure",! ! ! "mongodb"! ! ],! ! "comments" : [ ]! } Querying An Array

Slide 37

Slide 37 text

> db.article.update({_id: ! ! new ObjectId("51c3bcddfbd5d7261b4cdb5b")}, ! ! {$push:{comments:! ! {name: 'Steve Blank', comment: 'Awesome Post'}}})! > Using Update to Add a Comment

Slide 38

Slide 38 text

> db.article.findOne({_id: new ObjectId("51c3bcddfbd5d7261b4cdb5b")})! {! ! "_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"),! ! "body" : "This is my first blog post",! ! "comments" : [! ! ! {! ! ! ! "name" : "Steve Blank",! ! ! ! "comment" : "Awesome Post"! ! ! }! ! ],! ! "date" : ISODate("2013-06-20T00:00:00Z"),! ! "tags" : [! ! ! "adventure",! ! ! "mongodb"! ! ],! ! "title" : "Hello World",! ! "username" : "tgrall"! } Post with Comment Attached

Slide 39

Slide 39 text

@tgrall [email protected] Real applications are not built in the shell

Slide 40

Slide 40 text

MongoDB Drivers

Slide 41

Slide 41 text

@tgrall [email protected] MongoDB has native bindings for over 12 languages

Slide 42

Slide 42 text

Slide 43

Slide 43 text

Slide 44

Slide 44 text

@tgrall [email protected] Hey, we are at a JUG no?

Slide 45

Slide 45 text

@tgrall [email protected] Java & MongoDB • Java Driver • Morphia • Spring Data MongoDB • Hibernate OGM • Jongo

Slide 46

Slide 46 text

@tgrall [email protected] MongoDB Drivers (Java) • Manage Connections to MongoDB Cluster • Send commands over the wire • Serialize/Deserialize Java Objects to BSON • Generate Object ID

Slide 47

Slide 47 text

MongoClient mongoClient = new MongoClient("localhost", 27017);! DB db = mongoClient.getDB("ecommerce");! DBCollection collection = db.getCollection("orders");! DBObject order;! List items = new ArrayList();! DBObject item;! ! order = BasicDBObjectBuilder.start()! .add("customer", "Tug Grall")! .add("date", new Date())! .get();! item = BasicDBObjectBuilder.start()! .add("label", "MongoDB in Action")! .add("price", 13.30)! .add("qty", 1)! .get();! items.add(item);! ! order.put("items", items);! ! collection.insert(order); Java Driver : Insert

Slide 48

Slide 48 text

MongoClient mongoClient = new MongoClient("localhost", 27017);! ! DB db = mongoClient.getDB("ecommerce");! DBCollection collection = db.getCollection("orders");! ! ! DBObject query = BasicDBObjectBuilder.start()! .push("items.price")! .add(QueryOperators.GTE , 20)! .get();! ! ! DBCursor cursor = collection.find( query );! while (cursor.hasNext()) {! System.out.println(cursor.next());! } Java Driver : query

Slide 49

Slide 49 text

@tgrall [email protected] Morphia • Based on Java Driver • Provide a simple mapping • Query Builder • https://github.com/mongodb/morphia

Slide 50

Slide 50 text

public class Order {! ! @Id private ObjectId id;! ! private Date date;! ! @Property(“customer")! ! private String customer;! ! @Embedded! ! List items;! ! ...! }! ! {! Mongo mongo = new Mongo();! Morphia morphia = new Morphia();! Datastore datastore = morphia.createDatastore(mongo, “ecommerce”); ! ! Order order = new Order();! …! Key savedOrder = datastore.save(order);! … ! } Morphia: Insert

Slide 51

Slide 51 text

! ! ! ! List findByItemsQuantity(int quantity) {! ! ! return ! ! ! find( createQuery().filter("items.quantity", quantity))! ! ! .asList();! ! } Morphia : Query

Slide 52

Slide 52 text

@tgrall [email protected] ORM Object Relational Mapping

Slide 53

Slide 53 text

@tgrall [email protected] ODM Object Document Mapping

Slide 54

Slide 54 text

@tgrall [email protected] Spring Data • Part of the Spring ecosystem • Common approach for many datasources • RDBMS, NoSQL, Caching Layers • Key Features • Templating • ODM • Repository Support • http://projects.spring.io/spring-data-mongodb/

Slide 55

Slide 55 text

public class Order {! ! @Id private! ! String id;! ! private Date date;! ! @Field(“customer")! ! private String customer;! ! List items; ...! }! Spring Data: Insert

Slide 56

Slide 56 text

public interface OrderRepository ! ! extends MongoRepository {! ! ! List findByItemsQuantity(int quantity);! ! ! @Query("{ \"items.quantity\": ?0 }")! ! List findWithQuery(int quantity);! ! } Spring Data : Query

Slide 57

Slide 57 text

@tgrall [email protected] Jongo • Based on Java Driver • Document Mapping • Easy Query • MongoDB Shell “experience” for Java • http://jongo.org/

Slide 58

Slide 58 text

public class Order {! ! @Id private! ! String id;! ! private Date date;! ! @Field(“customer")! ! private String customerInfo;! ! List items; ...! }! ! {! MongoClient mongoClient = new MongoClient();! DB db = mongoClient.getDB("ecommerce");! Jongo jongo = new Jongo(db);! MongoCollection orders = jongo.getCollection("orders");! ! Order order = new Order()! ...! orders.save( order );! } Jongo: Insert

Slide 59

Slide 59 text

! {! ! DB db = mongoClient.getDB("ecommerce");! Jongo jongo = new Jongo(db);! MongoCollection orders = jongo.getCollection("orders");! ! Iterable result = orders! ! .find("{\"items.quantity\": #}", 2)! ! .fields( “{ _id :0, date : 1, customer : 1 }” );! ! .as(Order.class);! ! } Jongo: Query

Slide 60

Slide 60 text

@tgrall [email protected] Hibernate OGM • OGM : Object Grid Mapper • The “JPA” way • Subset of JPA • Query not yet well supported • Still under development • http://hibernate.org/ogm

Slide 61

Slide 61 text

public class Order {! ! @GeneratedValue(generator = "uuid")! ! @GenericGenerator(name = "uuid", strategy = "uuid2")! ! @Id private! ! String id;! ! private Date date;! ! @Column(name = “customer")! ! private String customer;! ! @ElementCollection! ! private List items;! …! }
 Hibernate OGM: Insert

Slide 62

Slide 62 text

{! … ! ! @PersistenceContext(unitName = "ecommerce-mongodb-ogm")! EntityManager em;! ! Order order = new Order();! …! ! em.persist(quote);! ! } Hibernate OGM: Insert

Slide 63

Slide 63 text

@tgrall [email protected] Which one is good for me?

Slide 64

Slide 64 text

@tgrall [email protected] MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM

Slide 65

Slide 65 text

@tgrall [email protected] MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM • Developed and Supported by MongoDB Inc • The most used way to use MongoDB & Java • Support “ all database features” • Too Verbose… (IMHO) • new version is coming 3.0

Slide 66

Slide 66 text

@tgrall [email protected] MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM • Developed and Supported by MongoDB Inc • Easy Mapping and Query • Active Community

Slide 67

Slide 67 text

@tgrall [email protected] MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM • Developed and Supported by Spring • Easy Mapping and Query • Advanced Features • Great for Spring developers!

Slide 68

Slide 68 text

@tgrall [email protected] MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM • Developed by the Community • Easy query and mapping • Mature • A better tools for MongoDB query language fans!

Slide 69

Slide 69 text

@tgrall [email protected] MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM • Developed by Red Hat - Jboss • Not yet supported • Under development (not mature, not for production!) • Too “relational” • nice to learn… but move away from it asap :)

Slide 70

Slide 70 text

Questions?

Slide 71

Slide 71 text

#ConferenceHashtag Thank You