Building Your First MongoDB Application

2,405 views

Published on

Introduction presentation delivered at ESPRIT JUG Day, Tunis 2014

Published in: Technology

Building Your First MongoDB Application

  1. 1. Technical Evangelist,MongoDB
 @tgrall Tugdual Grall @EspritJUG Building your first app;
 an introduction to MongoDB
  2. 2. @tgralltug@mongodb.com 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
  3. 3. @tgralltug@mongodb.com { “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 • tug@mongodb.com • tugdual@gmail.com
  4. 4. What is MongoDB?
  5. 5. @tgralltug@mongodb.com MongoDB is a ___________ database • Document • Open source • High performance • Horizontally scalable • Full featured
  6. 6. @tgralltug@mongodb.com Document Database • Not for .PDF & .DOC files • A document is essentially an associative array • Document = JSON object • Document = PHPArray • Document = Python Dict • Document = Ruby Hash • etc
  7. 7. @tgralltug@mongodb.com 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}! } }
  8. 8. @tgralltug@mongodb.com Open Source • MongoDB is an open source project • On GitHub • Licensed under the AGPL • Started & sponsored by 10gen • Commercial licenses available • Contributions welcome
  9. 9. @tgralltug@mongodb.com 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
  10. 10. @tgralltug@mongodb.com
  11. 11. @tgralltug@mongodb.com Database Landscape
  12. 12. @tgralltug@mongodb.com Full Featured • Ad Hoc queries • Real time aggregation • Rich query capabilities • Strongly consistent • Geospatial features • Support for most programming languages • Flexible schema
  13. 13. @tgralltug@mongodb.com 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
  14. 14. @tgralltug@mongodb.com mongodb.org/downloads
  15. 15. $ 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
  16. 16. 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
  17. 17. Document Database
  18. 18. @tgralltug@mongodb.com Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  19. 19. @tgralltug@mongodb.com Let’s Build a Blog
  20. 20. @tgralltug@mongodb.com First step in any application is Determine your entities
  21. 21. @tgralltug@mongodb.com Entities in our Blogging System • Users (post authors) • Article • Comments • Tags
  22. 22. @tgralltug@mongodb.com In a relational base app We would start by doing schema design
  23. 23. @tgralltug@mongodb.com Typical (relational) ERD
  24. 24. @tgralltug@mongodb.com In a MongoDB based app We start building our app and let the schema evolve
  25. 25. @tgralltug@mongodb.com MongoDB ERD
  26. 26. Working With MongoDB
  27. 27. @tgralltug@mongodb.com Demo time !
  28. 28. var user = { ! ! ! ! ! username: ’tgrall', ! ! ! ! ! first_name: ’Tugdual',! ! ! ! ! last_name: ’Grall',! } Start with an object 
 (or array, hash, dict, etc)
  29. 29. >db! test! > use blog! switching to db blog ! ! > db.users.insert( user ) Switch to Your DB
  30. 30. > db.users.insert(user) Insert the Record No collection creation necessary
  31. 31. > db.users.findOne()! {! ! "_id" : ObjectId("50804d0bd94ccab2da652599"),! ! "username" : ”tgrall",! ! "first_name" : ”Tugdual",! ! "last_name" : ”Grall"! } Find One Record
  32. 32. @tgralltug@mongodb.com _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
  33. 33. @tgralltug@mongodb.com 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
  34. 34. > 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
  35. 35. > 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
  36. 36. > 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
  37. 37. > db.article.update({_id: ! ! new ObjectId("51c3bcddfbd5d7261b4cdb5b")}, ! ! {$push:{comments:! ! {name: 'Steve Blank', comment: 'Awesome Post'}}})! > Using Update to Add a Comment
  38. 38. > 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
  39. 39. @tgralltug@mongodb.com Real applications are not built in the shell
  40. 40. MongoDB Drivers
  41. 41. @tgralltug@mongodb.com MongoDB has native bindings for over 12 languages
  42. 42. @tgralltug@mongodb.com
  43. 43. @tgralltug@mongodb.com
  44. 44. @tgralltug@mongodb.com Hey,we are at a JUG no?
  45. 45. @tgralltug@mongodb.com Java & MongoDB • Java Driver • Morphia • Spring Data MongoDB • Hibernate OGM • Jongo
  46. 46. @tgralltug@mongodb.com MongoDB Drivers (Java) • Manage Connections to MongoDB Cluster • Send commands over the wire • Serialize/Deserialize Java Objects to BSON • Generate Object ID
  47. 47. MongoClient mongoClient = new MongoClient("localhost", 27017);! DB db = mongoClient.getDB("ecommerce");! DBCollection collection = db.getCollection("orders");! DBObject order;! List<DBObject> items = new ArrayList<DBObject>();! 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
  48. 48. 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
  49. 49. @tgralltug@mongodb.com Morphia • Based on Java Driver • Provide a simple mapping • Query Builder • https://github.com/mongodb/morphia
  50. 50. public class Order {! ! @Id private ObjectId id;! ! private Date date;! ! @Property(“customer")! ! private String customer;! ! @Embedded! ! List<Item> items;! ! ...! }! ! {! Mongo mongo = new Mongo();! Morphia morphia = new Morphia();! Datastore datastore = morphia.createDatastore(mongo, “ecommerce”); ! ! Order order = new Order();! …! Key<Order> savedOrder = datastore.save(order);! … ! } Morphia: Insert
  51. 51. ! ! ! ! List<Order> findByItemsQuantity(int quantity) {! ! ! return ! ! ! find( createQuery().filter("items.quantity", quantity))! ! ! .asList();! ! } Morphia : Query
  52. 52. @tgralltug@mongodb.com ORM Object Relational Mapping
  53. 53. @tgralltug@mongodb.com ODM Object Document Mapping
  54. 54. @tgralltug@mongodb.com 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/
  55. 55. public class Order {! ! @Id private! ! String id;! ! private Date date;! ! @Field(“customer")! ! private String customer;! ! List<Item> items; ...! }! Spring Data: Insert
  56. 56. public interface OrderRepository ! ! extends MongoRepository<Order, String> {! ! ! List<Order> findByItemsQuantity(int quantity);! ! ! @Query("{ "items.quantity": ?0 }")! ! List<Order> findWithQuery(int quantity);! ! } Spring Data : Query
  57. 57. @tgralltug@mongodb.com Jongo • Based on Java Driver • Document Mapping • Easy Query • MongoDB Shell“experience”for Java • http://jongo.org/
  58. 58. public class Order {! ! @Id private! ! String id;! ! private Date date;! ! @Field(“customer")! ! private String customerInfo;! ! List<Item> 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
  59. 59. ! {! ! DB db = mongoClient.getDB("ecommerce");! Jongo jongo = new Jongo(db);! MongoCollection orders = jongo.getCollection("orders");! ! Iterable<Order> result = orders! ! .find("{"items.quantity": #}", 2)! ! .fields( “{ _id :0, date : 1, customer : 1 }” );! ! .as(Order.class);! ! } Jongo: Query
  60. 60. @tgralltug@mongodb.com Hibernate OGM • OGM : Object Grid Mapper • The“JPA”way • Subset of JPA • Query not yet well supported • Still under development • http://hibernate.org/ogm
  61. 61. 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<Item> items;! …! }
 Hibernate OGM: Insert
  62. 62. {! … ! ! @PersistenceContext(unitName = "ecommerce-mongodb-ogm")! EntityManager em;! ! Order order = new Order();! …! ! em.persist(quote);! ! } Hibernate OGM: Insert
  63. 63. @tgralltug@mongodb.com Which one is good for me?
  64. 64. @tgralltug@mongodb.com MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM
  65. 65. @tgralltug@mongodb.com 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
  66. 66. @tgralltug@mongodb.com MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM • Developed and Supported by MongoDB Inc • Easy Mapping and Query • Active Community
  67. 67. @tgralltug@mongodb.com MongoDB Java Driver Morphia Spring Data Jongo Hibernate OGM • Developed and Supported by Spring • Easy Mapping and Query • Advanced Features • Great for Spring developers!
  68. 68. @tgralltug@mongodb.com 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!
  69. 69. @tgralltug@mongodb.com 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 :)
  70. 70. Questions?
  71. 71. #ConferenceHashtag ThankYou

×