user data management with mongodb

32
Solutions Architect, MongoDB Heather Kirksey #MongoDB User Data Management with MongoDB

Upload: mongodb

Post on 06-May-2015

2.310 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: User Data Management with MongoDB

Solutions Architect, MongoDB

Heather Kirksey

#MongoDB

User Data Management with MongoDB

Page 2: User Data Management with MongoDB

Agenda

Page 3: User Data Management with MongoDB

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

MongoDB is a(n) ___________ database

• Document

• Open source

• High performance

• Horizontally scalable

• Full featured

Page 5: User Data Management with MongoDB

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

Data Modeling Exercise

• Insurance Company Data

• Account information– Name, address, etc– Account status– Notes

Page 7: User Data Management with MongoDB

Data Modeling Example

Page 8: User Data Management with MongoDB

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

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

Rule of Thumb

• Categories of data map well to MongoDB Collections

Policies Activities

Page 11: User Data Management with MongoDB

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

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

User Check-Ins

• Activity Streams

– Posts, tweets, check-ins– Recording user actions

Page 14: User Data Management with MongoDB

Places

Q: Current location A: Places near

location

User Generated Content

Places

Page 15: User Data Management with MongoDB

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

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

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

User Check Ins

Record User Check-Ins

Check-Ins

Users

Stats

Users

Stats

Page 19: User Data Management with MongoDB

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

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

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

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

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

Tracking Clicks

Policies Activities

Page 25: User Data Management with MongoDB

Each Click Creates a New Doc

Policies Activities Clicks

Page 26: User Data Management with MongoDB

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

Extending the Schema

user1 = {name: “John Smith”e-mail: “[email protected]”,check-ins:

[4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab]}

Page 28: User Data Management with MongoDB

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

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

Questions?

Page 31: User Data Management with MongoDB

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

Solutions Architect, MongoDB

Heather Kirksey

#MongoDB

Thank You