Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Your First MongoDB Application

Building Your First MongoDB Application

Introduction presentation delivered at ESPRIT JUG Day, Tunis 2014

Tugdual Grall

May 07, 2014

Transcript

71 slides
  1. @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
  2. @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]
  3. @tgrall [email protected] MongoDB is a ___________ database • Document •

    Open source • High performance • Horizontally scalable • Full featured
  4. @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
  5. @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}! } }
  6. @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
  7. @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
  8. @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
  9. @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
  10. 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
  11. @tgrall [email protected] Terminology RDBMS MongoDB Table, View ➜ Collection Row

    ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  12. var user = { ! ! ! ! ! username:

    ’tgrall', ! ! ! ! ! first_name: ’Tugdual',! ! ! ! ! last_name: ’Grall',! } Start with an object 
 (or array, hash, dict, etc)
  13. >db! test! > use blog! switching to db blog !

    ! > db.users.insert( user ) Switch to Your DB
  14. > db.users.findOne()! {! ! "_id" : ObjectId("50804d0bd94ccab2da652599"),! ! "username" :

    ”tgrall",! ! "first_name" : ”Tugdual",! ! "last_name" : ”Grall"! } Find One Record
  15. @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
  16. @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
  17. > 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
  18. > 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
  19. > 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
  20. > db.article.update({_id: ! ! new ObjectId("51c3bcddfbd5d7261b4cdb5b")}, ! ! {$push:{comments:! !

    {name: 'Steve Blank', comment: 'Awesome Post'}}})! > Using Update to Add a Comment
  21. > 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
  22. @tgrall [email protected] Java & MongoDB • Java Driver • Morphia

    • Spring Data MongoDB • Hibernate OGM • Jongo
  23. @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
  24. 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
  25. 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
  26. @tgrall [email protected] Morphia • Based on Java Driver • Provide

    a simple mapping • Query Builder • https://github.com/mongodb/morphia
  27. 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
  28. ! ! ! ! List<Order> findByItemsQuantity(int quantity) {! ! !

    return ! ! ! find( createQuery().filter("items.quantity", quantity))! ! ! .asList();! ! } Morphia : Query
  29. @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/
  30. public class Order {! ! @Id private! ! String id;!

    ! private Date date;! ! @Field(“customer")! ! private String customer;! ! List<Item> items; ...! }! Spring Data: Insert
  31. public interface OrderRepository ! ! extends MongoRepository<Order, String> {! !

    ! List<Order> findByItemsQuantity(int quantity);! ! ! @Query("{ \"items.quantity\": ?0 }")! ! List<Order> findWithQuery(int quantity);! ! } Spring Data : Query
  32. @tgrall [email protected] Jongo • Based on Java Driver • Document

    Mapping • Easy Query • MongoDB Shell “experience” for Java • http://jongo.org/
  33. 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
  34. ! {! ! 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
  35. @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
  36. 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
  37. {! … ! ! @PersistenceContext(unitName = "ecommerce-mongodb-ogm")! EntityManager em;! !

    Order order = new Order();! …! ! em.persist(quote);! ! } Hibernate OGM: Insert
  38. @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
  39. @tgrall [email protected] MongoDB Java Driver Morphia Spring Data Jongo Hibernate

    OGM • Developed and Supported by MongoDB Inc • Easy Mapping and Query • Active Community
  40. @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!
  41. @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!
  42. @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 :)

More Decks by Tugdual Grall

Other Decks in Technology