building your first mongodb application
Post on 18-Oct-2014
576 views
DESCRIPTION
Introduction presentation delivered at ESPRIT JUG Day, Tunis 2014TRANSCRIPT
Technical Evangelist, MongoDB@tgrall
Tugdual Grall
@EspritJUG
Building your first app;an introduction to MongoDB
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
{ “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
What is MongoDB?
MongoDB is a ___________ database
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
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
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}! } }
Open Source
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• Started & sponsored by 10gen
• Commercial licenses available
• Contributions welcome
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
Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Strongly consistent
• Geospatial features
• Support for most programming languages
• Flexible schema
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
$ 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
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
Document Database
Terminology
RDBMS MongoDB
Table, View ➜ Collection
Row ➜ Document
Index ➜ Index
Join ➜ Embedded Document
Foreign Key ➜ Reference
Partition ➜ Shard
First step in any application is
Determine your entities
Entities in our Blogging System
• Users (post authors)
• Article
• Comments
• Tags
In a relational base app
We would start by doing schema design
In a MongoDB based appWe start building our appand let the schema evolve
Working With MongoDB
var user = { !! ! ! ! username: ’tgrall', !! ! ! ! first_name: ’Tugdual',!! ! ! ! last_name: ’Grall',!}
Start with an object (or array, hash, dict, etc)
>db!test!> use blog! switching to db blog !!> db.users.insert( user )
Switch to Your DB
> db.users.insert(user)
Insert the Record
No collection creation necessary
> db.users.findOne()!{!! "_id" : ObjectId("50804d0bd94ccab2da652599"),!! "username" : ”tgrall",!! "first_name" : ”Tugdual",!! "last_name" : ”Grall"!}
Find One Record
_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
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
> 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
> 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
> 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
> db.article.update({_id: !
! new ObjectId("51c3bcddfbd5d7261b4cdb5b")}, !
! {$push:{comments:!
! {name: 'Steve Blank', comment: 'Awesome Post'}}})!
>
Using Update to Add a Comment
> 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
MongoDB Drivers
Java & MongoDB
• Java Driver
• Morphia
• Spring Data MongoDB
• Hibernate OGM
• Jongo
MongoDB Drivers (Java)
• Manage Connections to MongoDB Cluster
• Send commands over the wire
• Serialize/Deserialize Java Objects to BSON
• Generate Object ID
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
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
Morphia
• Based on Java Driver
• Provide a simple mapping
• Query Builder
• https://github.com/mongodb/morphia
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
! !!! List<Order> findByItemsQuantity(int quantity) {!! ! return !! ! find( createQuery().filter("items.quantity", quantity))!! ! .asList();!! }
Morphia : Query
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/
public class Order {!! @Id private!! String id;!! private Date date;!! @Field(“customer")!! private String customer;!! List<Item> items; ...!}!
Spring Data: Insert
public interface OrderRepository !! extends MongoRepository<Order, String> {!!! List<Order> findByItemsQuantity(int quantity);!!! @Query("{ \"items.quantity\": ?0 }")!! List<Order> findWithQuery(int quantity);!!}
Spring Data : Query
Jongo
• Based on Java Driver
• Document Mapping
• Easy Query
• MongoDB Shell “experience” for Java
• http://jongo.org/
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
!{!!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
Hibernate OGM
• OGM : Object Grid Mapper
• The “JPA” way
• Subset of JPA
• Query not yet well supported
• Still under development
• http://hibernate.org/ogm
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
{!… !!@PersistenceContext(unitName = "ecommerce-mongodb-ogm")!EntityManager em;!!Order order = new Order();!…!!em.persist(quote);!!}
Hibernate OGM: Insert
MongoDB Java Driver
Morphia Spring Data Jongo Hibernate OGM
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
MongoDB Java Driver
Morphia Spring Data Jongo Hibernate OGM
• Developed and Supported by MongoDB Inc • Easy Mapping and Query • Active Community
MongoDB Java Driver
Morphia Spring Data Jongo Hibernate OGM
• Developed and Supported by Spring • Easy Mapping and Query • Advanced Features • Great for Spring developers!
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!
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 :)
Questions?
#ConferenceHashtag
Thank You