MongoDB + Spring
Norberto Leite
@nleite
http://www.mongodb.com/norberto
Two leafs of the same tree
3
Agenda
• MongoDB Introduction
– Just in case you've been distracted
• Spring Framework Overview
• Spring + MongoDB
– Spring Data
– Spring Boot
– Spring Batch
4
Ola, I'm Norberto!
Norberto Leite
Technical Evangelist
Madrid, Spain
@nleite
http://www.mongodb.com/norberto
MongoDB
6
MongoDB
GENERAL PURPOSE DOCUMENT DATABASE OPEN-SOURCE
7
MONGODB FEATURES
JSON Document Model
with Dynamic Schemas
Auto-Sharding for
Horizontal Scalability
Text Search
Aggregation Framework
and MapReduce
Full, Flexible Index Support
and Rich Queries
Built-In Replication
for High Availability
Advanced Security
Large Media Storage
with GridFS
8
THE LARGEST ECOSYSTEM
9,000,000+MongoDB Downloads
250,000+Online Education Registrants
35,000+MongoDB User Group Members
35,000+MongoDB Management Service (MMS) Users
750+Technology and Services Partners
2,000+Customers Across All Industries
Document Data Model
Relational MongoDB
{
first_name: ‘Paul’,
surname: ‘Miller’,
city: ‘London’,
location:
[45.123,47.232],
cars: [
{ model: ‘Bentley’,
year: 1973,
value: 100000, … },
{ model: ‘Rolls Royce’,
year: 1965,
value: 330000, … }
]
}
10
Documents are Rich Data Structures
{
first_name: ‘Paul’,
surname: ‘Miller’,
cell: ‘+447557505611’
city: ‘London’,
location: [45.123,47.232],
Profession: [banking, finance, trader],
cars: [
{ model: ‘Bentley’,
year: 1973,
value: 100000, … },
{ model: ‘Rolls Royce’,
year: 1965,
value: 330000, … }
]
}
Fields can contain an array of sub-
documents
Fields
Typed field values
Fields can contain
arrays
11
Document Model Benefits
Agility and flexibility
Data model supports business change
Rapidly iterate to meet new requirements
Intuitive, natural data representation
Eliminates ORM layer
Developers are more productive
Reduces the need for joins, disk seeks
Programming is more simple
Performance delivered at scale
{
_id : ObjectId("4c4ba5e5e8aabf3"),
employee_name: "Dunham, Justin",
department : "Marketing",
title : "Product Manager, Web",
report_up: "Neray, Graham",
pay_band: “C",
benefits : [
{ type : "Health",
plan : "PPO Plus" },
{ type : "Dental",
plan : "Standard" }
]
}
Dynamic Schema
{
policyNum: 123,
type: auto,
customerId: abc,
payment: 899,
deductible: 500,
make: Taurus,
model: Ford,
VIN: 123ABC456,
}
{
policyNum: 456,
type: life,
customerId: efg,
payment: 240,
policyValue: 125000,
start: jan, 1995
end: jan, 2015
}
{
policyNum: 789,
type: home,
customerId: hij,
payment: 650,
deductible: 1000,
floodCoverage: No,
street: “10 Maple Lane”,
city: “Springfield”,
state: “Maryland”
}
Query Operators
Conditional Operators $all, $exists, $mod, $ne, $in, $nin, $nor, $or, $size,
$type
$lt, $lte, $gt, $gte
// find customers with any claims
> db.customers.find( {claims: {$exists: true }} )
// find customers matching a regular expression
> db.customers.find( {last: /^rog*/i } )
// count customers by city
> db.customers.find( {city: ‘Philadelphia’} ).count()
14
Indexes
// Index nested documents
> db.customers.ensureIndex({“policies.agent”:1} )
> db.customers.find({‘policies.agent’:’Fred’})
// geospatial index
> db.customers.ensureIndex({“property.location”: “2d” } )
> db.customers.find({“property.location” : { $near : [22,42] }} )
// text index
> db.customers.ensureIndex({“policies.notes”: “text” } )
MongoDB is Fully Featured
Spring Framework
Not just a framework!
17
Spring Framework
Spring Projects
Spring & MongoDB
Same Tree?
Let's put these 2 to work!
Build a "cool" App
24
Nice and simple
25
Video Catalog App
• All videos from our Education platform
– Yes, we have the coolest framework ever for
remote education!
https://university.mongodb.com/
26
Video Catalog App
• All videos from our Education platform
– Yes, we have the coolest framework ever for
remote education!
• Load information into MongoDB
• Allow people to vote on videos
• Find videos based on the metadata
• All using Spring Projects
– Let's also look into optimizations
27
Stack
MongoDB
DAL – Database Access Layer
demo.springio.batch REST API
CODE, CODE, CODE
29
DAL
• Decoupling Database
– Rule n1 on good Software Development
• Base Library
• Simple
30
Batch
• Batch Configuration Class
– reader()
– writer()
@Configuration
@EnableBatchProcessing
• ItemProcessor()
– Excellent way to do pre-aggregations,
computations…
31
Batch
• Things to look for
– chunk(chunkSize)
• Keep an eye on this value to optmise the loading
process
– writer()
• MongoDB bulk insert is here to help
32
REST API
@RestController
@RequestMapping("/…")
@EnableAutoConfiguration
… extends MongoRepository<T, I>
33
REST API
• Things to have in mind
– MongoRepository is "just" CRUD repository
• Need to autowire MongoTemplate to aggregate
– Updates
• Not a particular issue of SpringData
• General thing of ODM's
http://1.bp.blogspot.com/-H40bE-rJUqk/UXo3YatYjqI/AAAAAAAAAho/zjrQVk3WrNA/s1600/paella_03.jpg
Take Ways
35
Recap
• Spring has a lot of things out of the box that we do not
need to reinvent the all the time
• MongoDB can easily be integrated with existing Spring
based projects
• Performance is important
• Continuous Improvement is key!
36
Recap
• DOCUMENTATION!
– Awesome and complete on both projects!
We love enterprise!
38
MongoDB 3.0 is here!
http://www.humanandnatural.com/data/media/178/badan_jaran_desert_oasis_china.jpghttp://www.tinypm.com/blog/wp-content/uploads/2015/01/hammer.jpg
Fully Available!
3.0.2 https://www.mongodb.org/downloads
http://www.mongodb.com/norberto
40
Register now: mongodbworld.com
Early Bird Ends May 1!
Use Code NorbertoLeite for additional 25% Off*Come as a group of 3 or more – Save another 25%
We’re Always Looking for Top Talent
What are employees saying?
“Working with a group of individuals who you know will have your back is
one of the reasons I love working at MongoDB”
“Every day, we get to solve hard problems that make distributed databases
more accessible to developers all over the world”
“MongoDB lets you tackle real problems that affect hundreds of thousands
of users”
Visit us at www.mongodb.com/careers to see a full
list of opportunities or email your resume to
What are we hiring for?
• Technical Services Engineers (Dublin)
• Consulting Engineers (UK OR France)
• Solution Architects (France, Spain, Germany)
• Enterprise Account Executives ( France, Italy, UK, Germany)
• Corporate Account Executives (Dublin)
• Renewals Account Managers (Dublin)
Same Tree!!!
http://www.humanandnatural.com/data/media/178/badan_jaran_desert_oasis_china.jpg
Questions?
@nleite