This document discusses building a location-based check-in app using MongoDB. It begins with an introduction to MongoDB's key features like being document-oriented, open source, high performance, and horizontally scalable. It then demonstrates modeling the app's entities of users, places, and check-ins as MongoDB collections. It shows examples of queries, indexes, and updates used in the app. The document concludes by discussing more advanced features like aggregation and deployment options for MongoDB.
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
Webinar: Building Your First Application with MongoDB
1. Building your First App with
MongoDB
Blossom Coryat
Solutions Architect, 10gen
2. Today‟s Talk
• Introduction to MongoDB
• Discuss data modeling, queries, indexes,
updates and aggregation in MongoDB within
the context of building a location-based app
4. MongoDB is a ___________
database
• Document Oriented
• Open source
• High performance
• Highly Available
• Horizontally scalable
• Full featured
5. Document Oriented
• A document is essentially an associative array
• Document == JSON object
• Document == PHP Array
• Document == Python Dict
• Document == Ruby Hash
• Etc.
6. Open Source
• MongoDB is an open source project
– Started and maintained by 10gen
• Licensed under the AGPL
• Commercial licenses available
• On GitHub
• Contributions welcome
7. High Performance
• Written in C++
• Runs nearly everywhere
• Extensive use of memory-mapped files
• Data serialized as BSON (fast parsing)
• Full support for primary & secondary indexes
• Document model = less work
8. Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Flexible schema
• Geospatial features
• Support for most programming languages
9. MongoDB drivers
• Official Support for 12 languages
• Community drivers for tons more
• Drivers connect to mongo servers
• Drivers translate BSON into native types
• Installed using typical means (npm, pecl, gem,
pip)
22. Requirements
• Users should be able to check in to a location
• Should be able to view check-ins by location
• Ability to assign categories to places
• Find nearby places
• Generate check-in statistics
23. First step in any application is
Determine your entities
25. In a relational based app
We would start by doing
schema design
26. Relational schema design
• Large entity relationship diagrams
• Complex create table statements
• Tables just to join tables together
• ORMs to map tables to objects
• Lots of revisions until we get it just right
27. In a MongoDB based app
We start building our
and let the schema evolve
app
28. MongoDB collections
• Users
• Places
• Check-ins
– Separate collection to manage discrete check-in events
and support high workload of writes
32. Querying for the user
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"firstname" : "fred",
"lastname" : "jones"
}
33. _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
34. ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599")
|---------||-----||-----||--------|
ts mac pid inc
40. Indexes
• Can be created on any field
– Including fields within sub-documents or embedded arrays
• Support for compound indexes on any
combination of keys
• Indexes should exist for primary, common, and
user-facing queries
43. Updating
> db.checkins.insert({
place_id: pid,
user_id: uid,
datetime: new Date()
})
// update the “checkins” count on place and user
// “checkins” will be added if it doesn‟t exist
> db.places.update({_id: pid}, {$inc: {checkins: 1}})
> db.users.update({_id: uid}, {$inc: {checkins: 1}})
44. Simple Statistics
// find the last 10 checkins for a place:
> p = db.places.find({name:”Exploratorium”})
> db.checkins.find({place_id:p[„_id‟]})
.sort({datetime:-1}).limit(10)
// how many people checked in today
> db.checkins.find({place_id:p[„_id‟],
datetime: {$gt: midnight}}).count()
45. Aggregation Framework
• Documents pass through a pipeline of
operations
• Mechanism to calculate aggregate values across
your data
• Similar functionality to GROUP BY and related
SQL operators
• Along with computation, data can be reshaped
– Add computed fields
– Create virtual sub-objects
– Extract sub-fields
51. Deployment
• Single server
- need a strong backup plan
P
• Replica sets P S S
- High availability
- Automatic failover
52. Deployment
• Single server
- need a strong backup plan
P
• Replica sets P S S
- High availability
- Automatic failover
P S S
• Sharded
- Horizontally scale
- Auto balancing P S S