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
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. @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
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. @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
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. @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. > 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. > 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
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. public class Order {!
! @Id private!
! String id;!
! private Date date;!
! @Field(“customer")!
! private String customer;!
! List<Item> items; ...!
}!
Spring Data: Insert
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. !
{!
!
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. @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
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
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 :)