user data management with mongodb
TRANSCRIPT
![Page 1: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/1.jpg)
Solutions Architect, MongoDB
Heather Kirksey
#MongoDB
User Data Management with MongoDB
![Page 2: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/2.jpg)
Agenda
![Page 3: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/3.jpg)
Agenda• High Level Overview
– MongoDB– User Data
• Modeling & Querying User Data– Insurance Company User Data– User Check-Ins
• Extending the Data Model for Future Use Cases
– Tracking User Activity– Social Media
![Page 4: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/4.jpg)
MongoDB is a(n) ___________ database
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
![Page 5: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/5.jpg)
User Data• Account Information
– Name, address, etc.– Account status– Notes
• Activity Streams– Posts, tweets, likes, check-ins– Recording user actions
• Social Networks– Friends, connections– Groups, tags
![Page 6: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/6.jpg)
Data Modeling Exercise
• Insurance Company Data
• Account information– Name, address, etc– Account status– Notes
![Page 7: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/7.jpg)
Data Modeling Example
![Page 8: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/8.jpg)
Data Modeling Example
User Opened
Date: 05/26/2013Status: Successful
Account Modified
Date: 06/04/2013Status: Added spouse
User Call Log
Date: 01/22/2014Type: Complaint
![Page 9: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/9.jpg)
Insurance Company Site
User Opened
Date: 05/26/2013Status: Successful
Account Modified
Date: 06/04/2013Status: Added spouse
User Call Log
Date: 01/22/2014Type: Complaint
2 types of data
2 types of data
![Page 10: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/10.jpg)
Rule of Thumb
• Categories of data map well to MongoDB Collections
Policies Activities
![Page 11: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/11.jpg)
Policiespolicy = {
name: “John Smith”
employer: “10gen”,
address: “555 Fictional Ave”,
e-mail: “[email protected]”,
spouse: “Yes” ,
dependents: “No”,
dates: [
{start: 5/26/2013 10:12:00},
end: 5/26/2023 10:12:00}],
others: “No”}
![Page 12: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/12.jpg)
Activities
activity = {user-id:
“JohnSmith421”type: “account-
opening” ,status: “Success”,dates: 5/26/2013
10:12:00,related-doc:
“/customer/JohnSmith421/open.pdf” }
User Opened AccountDate: 05/26/2013
Status: Success
Account ModifiedDate: 06/04/2013
Action: Added Spouse
User Call LogDate: 01/22/2014
Type: Complaint
![Page 13: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/13.jpg)
User Check-Ins
• Activity Streams
– Posts, tweets, check-ins– Recording user actions
![Page 14: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/14.jpg)
Places
Q: Current location A: Places near
location
User Generated Content
Places
![Page 15: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/15.jpg)
Inserting a Place
var p = { name: “MongoDB HQ”,
address: “229 W 43rd St”,
city: “New York”,
zip: “10036”,
tags: [“mongoDB”, “business”],
latlong: [40.0, 72.0],
tips: [{user: “John Smith”, time: “3/15/2013”,tip: “Make
sure to stop by for office hours!”}]}
> db.posts.save(p)
![Page 16: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/16.jpg)
Updating Tips
db.places.update({name: “MongoDB HQ”},
{$push :{tips:
{user: “John”, time:3/10/2014, tip: “stop by for office hours on Wednesdays from 4-6”}}})
![Page 17: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/17.jpg)
Querying Our Places• Creating Indexes
• db.places.ensureIndex({tags:1})
• db.places.ensureIndex({name:1})
• db.places.ensureIndex({latlong:”2d”})
• Finding Places
• db.places.find({latlong:{$near:[40,70]}})
• Regular Expressions
• db.places.find({name: /^typeaheadstring/)
• Using Tags
• db.places.find({tags: “business”})
![Page 18: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/18.jpg)
User Check Ins
Record User Check-Ins
Check-Ins
Users
Stats
Users
Stats
![Page 19: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/19.jpg)
Users
user1 = {name: “John Smith”e-mail: “[email protected]”,check-ins:
[4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab]}
checkins [] = ObjectId reference to Check-Ins Collection
![Page 20: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/20.jpg)
Check-Ins
user1 = {place: “MongoDB HQ”,ts: 9/20/2010 10:12:00,userId: <object id of user>
}
Every Check-In is Two Operations• Insert a Check-In Object (check-ins collection)• Update ($push) user object with check-in ID (users collection)
![Page 21: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/21.jpg)
Simple Stats
db.checkins.find({place: “MongoDB HQ”)
db.checkins.find({place: “MongoDB HQ”}).sort({ts:-1}).limit(10)
db.checkins.find({place: “MongoDB HQ”, ts: {$gt: midnight}}).count()
![Page 22: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/22.jpg)
Stats w/ MapReduce
mapFunc = function() {emit(this.place, 1);}
reduceFunc = function(key, values) {return Array.sum(values);}
res = db.checkins.mapReduce(mapFunc,reduceFunc, {query: {timestamp: {$gt:nowminus3hrs}}})
res = [{_id:”MongoDB HQ”, value: 17}, … , …]
…or try using the aggregation framework!
![Page 23: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/23.jpg)
Adding More User Data
User Opened
Date: 05/26/2013Status: Successful
Account Modified
Date: 06/04/2013Status: Added spouse
User Call Log
Date: 01/22/2014Type: Complaint
![Page 24: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/24.jpg)
Tracking Clicks
Policies Activities
![Page 25: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/25.jpg)
Each Click Creates a New Doc
Policies Activities Clicks
![Page 26: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/26.jpg)
Clicks
click = {
user: “JohnSmith”,
ts: 9/20/2010 10:12:00,
link: “http://some-link-here.com/wherever”}
Now we can audit user activity...
db.clicks.find({user:”JohnSmith”}).sort({ts:-1})
Show me all of John’s clicks sorted by timestamp.
![Page 27: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/27.jpg)
Extending the Schema
user1 = {name: “John Smith”e-mail: “[email protected]”,check-ins:
[4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab]}
![Page 28: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/28.jpg)
Extending the Schema
user1 = {name: “John Smith”e-mail: “[email protected]”,check-ins:
[4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab]}
friends: [7b47j62bk1d3c5621c1icv90, 1h11p62bf1d8c716za]}
![Page 29: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/29.jpg)
Takeaways• User data fits well in MongoDB
– Flexible data model– Stay agile; make changes– Many customers in production
• Application patterns drive data design– Optimize data model for queries– Primary use cases drive design
• Adding features is easy– Create new data structures– Extend existing
![Page 30: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/30.jpg)
Questions?
![Page 31: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/31.jpg)
MongoDB WorldNew York City, June 23-25
#MongoDBWorld
See what’s next in MongoDB including • MongoDB 2.6• Sharding• Replication• Aggregation
http://world.mongodb.comSave $200 with discount code THANKYOU
![Page 32: User Data Management with MongoDB](https://reader035.vdocuments.mx/reader035/viewer/2022081421/554a3d52b4c905293a8b4da4/html5/thumbnails/32.jpg)
Solutions Architect, MongoDB
Heather Kirksey
#MongoDB
Thank You