2. Today’s webinar
• Introduction to MongoDB.
• Use a location-based surf report app as an
example.
• Basic data modelling, queries (inc
geospatial) and aggregation.
• MongoDB JS shell and Java code examples.
3. What is MongoDB?
• MongoDB is a scalable, high-performance,
open source NoSQL database.
• Document-oriented storage
• Full index support - including geospatial
• Querying
• Fast in-place updates
• Map-Reduce and Aggregation
• Replication and high availability
• Horizontal scaling with auto-sharding
• GridFS
4. Open source
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• Started & sponsored by 10gen
• Commercial licenses available
• Contributions welcome
5. 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
16. Java: DBObjects
BasicDBObject report = new BasicDBObject();
report.put("reporter", "matt");
report.put("date", new Date());
BasicDBObject location = new BasicDBObject();
location.put("name", "Watergate Bay");
location.put("county", "Cornwall");
location.put("country", "UK");
location.put("coordinates", new double[] {-50.46667, 5.05});
report .put("location", location);
BasicDBObject conditions = new BasicDBObject();
conditions .put("height", 3);
conditions .put("period", 13);
conditions.put("rating", 5);
report .put("conditions", conditions);
17. Connecting to MongoDB
Mongo m = new Mongo(Arrays.asList(
new ServerAddress("localhost", 27017),
new ServerAddress("localhost", 27018),
new ServerAddress("localhost", 27019)));
DB db = m.getDB( ”SurfReports” );
DBCollection coll = db. getCollection(“reports”);
20. Get local surf conditions - shell
> db.reports.find(
{
"location.coordinates" : { $near : [50.41, -5.08] ,
$maxDistance : 0.5},
date : { $gte : new Date(2012, 11, 21)}
},
{"location.name" :1, _id : 0, "conditions" :1}
).sort({"conditions.rating" : -1})
• Get local reports
21. Get local surf conditions - shell
> db.reports.find(
{
"location.coordinates" : { $near : [50.41, -5.08] ,
$maxDistance : 0.5},
date : { $gte : new Date(2012, 11, 21)}
},
{"location.name" :1, _id : 0, "conditions" :1}
).sort({"conditions.rating" : -1})
• Get local reports
• Get today’s reports
22. Get local surf conditions - shell
> db.reports.find(
{
"location.coordinates" : { $near : [50.41, -5.08] ,
$maxDistance : 0.5},
date : { $gte : new Date(2012, 11, 21)}
},
{"location.name" :1, _id : 0, "conditions" :1}
).sort({"conditions.rating" : -1})
• Get local reports
• Get today’s reports
• Return only the relevant info
23. Get local surf conditions - shell
> db.reports.find(
{
"location.coordinates" : { $near : [50.41, -5.08] ,
$maxDistance : 0.5},
date : { $gte : new Date(2012, 11, 21)}
},
{"location.name" :1, _id : 0, "conditions" :1}
).sort({"conditions.rating" : -1})
• Get local reports
• Get today’s reports
• Return only the relevant info
• Show me the best surf first
26. Analysis feature:
Aggregation framework
What are the best conditions for my beach?
27. Aggregation framework
• Process a stream of documents
• Original input is a collection
• Final output is a result document
• Series of operators
• Filter or transform data
• Input/output chain
ps ax | grep mongod | head -n 1
28. Pipelining operations
$match Match “Fistral North”
$project Only interested in conditions
Group by rating, average
$group
wave height and wave period
$sort Order by best conditions
40. Sharding
• Automatic partitioning and management
• Range based
• Convert to sharded system with no downtime
• Fully consistent
• No code changes required
41. Scaling MongoDB
MongoDB
Single Instance
or
Replica Set
Client
Application
50. More information
Resource Location
MongoDB Downloads www.mongodb.org/download
Free Online Training education.10gen.com
Webinars and Events www.10gen.com/events
White Papers www.10gen.com/white-papers
Customer Case Studies www.10gen.com/customers
Presentations www.10gen.com/presentations
Documentation docs.mongodb.org
Additional Info info@10gen.com