MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business...

56
From Story to Document APPLYING MONGO ORIENTED THINKING TO BUSINESS REQUIREMENTS NURI HALPERIN | MONGODB DAYS SILICON VALLEY 2015

Transcript of MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business...

Page 1: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

From Story to DocumentAPPLYING MONGO ORIENTED THINKING TO BUSINESS REQUIREMENTS

NURI HALPERIN | MONGODB DAYS SILICON VALLEY 2015

Page 2: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Why are we here?

Page 3: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

MongoDB SQL

Square pegs, round holes

Experience

Habits

Document

Page 4: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

From Tables to Documents

Document

Page 5: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

A team story

Page 6: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Good News Everyone!

Page 7: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

"Maker Space"

Page 8: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

"Victory!"

Page 9: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Let's plan this!

Page 10: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Tables!Constraints!

Normalization!

Page 11: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Person

id UUID

first_name nvarchar(32)

last_name nvarchar(32)

Page 12: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Makers are People!

Page 13: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Person

id UUID

first_name nvarchar(32)

last_name nvarchar(32)

Maker

person_id UUID

status_id Int (not null)

Maker Status

id int

status varchar(16)

Page 14: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Tool

id UUID

name nvarchar(32)

Page 15: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Tool

id UUID

name nvarchar(32)

tool_type_id int

Tool Type

id int

name vharchar

"hand" | "power"

Page 16: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Is everything a tool?

Page 17: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Tool

asset_id UUID

tool_type_id int

Tool Type

id int

name vharchar

is_certifiable bit (not null)

Asset

id UUID

name nvarchar(32)

is_stationary bit

Page 18: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Certification

id UUID

tool_type_id int

date datetime

Maker Certification

member_id UUID

certification_id UUID

date datetime

Person

id UUID

first_name nvarchar(32)

last_name nvarchar(32)

Tool Type

id int

name vharchar

is_certifiable bit (not null)

Page 19: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Tool

asset_id UUID

tool_type_id int

Tool Type

id int

name vharchar

Asset

id UUID

name nvarchar(32)

is_stationary bit

Certificationperson_id UUIDtool_type_id UUIDname varchar(16)

Member Certification

member_id UUIDcertification_id UUIDdate datetime

Member Tool Log

asset_id UUID

type_id int

Person

id UUID

first_name nvarchar(32)

last_name nvarchar(32)

Maker

person_id UUID

status_id Int (not null)

Maker Status

id int

status varchar(16)

Page 20: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Must. Not. Overthink.

Page 21: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Maker gets certified

Borrow & return tool

Report tool usage

Page 22: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Key interactions drivesdocument design.

Page 23: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Tool

asset_id UUID

type_id int

Tool Type

id int

name vharchar

Asset

id UUID

name nvarchar(32)

is_stationary bit

Certificationperson_id UUIDasset_id UUIDname varchar(16)

Member Certification

member_id UUIDcertification_id UUID

Member Tool Logasset_id UUIDtype_id int

date datetime

Person

id UUID

first_name nvarchar(32)

last_name nvarchar(32)

Maker

person_id UUID

status_id Int (not null)

Maker Status

id int

status varchar(16)

We can do anything!

Page 24: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Generic Purpose-Built

Speed

Efficiency

Scale

Agility

Complexity

Performance

Cost

Page 25: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: ObjectId('…'),name: 'Martha',

}

"Person"

Page 26: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: ObjectId('…'),name: 'Martha',status: 'paid'

}

"Person"

"Maker"

Page 27: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: ObjectId('…'),name: 'Martha',status: 'paid',certified: [{t:'cnc', d:'2015-11-

26'}],}

Get Certified

Page 28: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: ObjectId('…'),name: 'Martha',status: 'paid',certified: [{t:'cnc', d:'2015-11-

26'}],}

db.maker.update({name: 'Martha', 'certified.t':

{$ne:'cnc'}},{ $push:

{certified: {t:'cnc', d: ISODate()} }

}

Page 29: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: ObjectId('…'),name: 'Martha',status: 'paid',certified: [{t:'cnc', d:'2015-11-

26'}],}

Certification

id UUID

person_id UUID

tool_type_id int

date datetime

Maker Certification

member_id UUID

certification_id UUID

Person

id UUID

first_name nvarchar(32)

last_name nvarchar(32)

Page 30: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Data that works together lives together.

Page 31: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Tables!Constraints!

Normalization!

Page 32: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

if ( tool.type != 'hand') {alert('Certification

Required');}

{ _id: 1, name: 'knife', type: 'hand'}{ _id: 2, name: 'drill', type: 'power'}{ _id: 3, name: 'lathe', type: 'stationary'}

Page 33: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

if ( tool.type != 'hand') {alert('Certification

Required');}

{ _id: 1, name: 'knife', type: 'hand'}{ _id: 2, name: 'drill', type: 'power'}{ _id: 3, name: 'lathe', type: 'stationary'}{ _id: 4, name: 'scope', type: 'instrument'}

Careful!

X

Page 34: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{ _id: 1, name: 'knife', …, req: false}{ _id: 2, name: 'drill', …, req: true}{ _id: 3, name: 'lathe', …, req: true}{ _id: 4, name: 'scope', …, req: true}

There, I fixed it!

if (tool.requiresCertification) {alert('Certification

Required');}

Page 35: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{ _id: 1, name: 'knife', type: 'hand'}{ _id: 2, name: 'drill', type: 'power'}{ _id: 3, name: 'lathe', type: 'stationary'}{ _id: 4, name: 'scope', type: 'instrument'}X

public enum ToolType {HAND, POWER, STATIONARY

};

OOP, Oops!

Page 36: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Extra Field :: TablesDOWN TIME? COORDINATED DEPLOYMENT?

ALTER TABLE [x]

ADD COLUMN [is_certifiable] bit

DEFAULT 0

Page 37: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Extra Field :: MongoDBFLEXIBLE SCHEMA, CODE FIRST

> db.x.insert({ …, is_certifiable: true})

> db.x.update( {_id: 123}, {$set:{is_certifiable: true}})

On the fly!

Page 38: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Embed immutable data.

Page 39: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: 1, name: 'scope', borrows: [

{ maker_id: 1, dt: ISODate('2015-06-12'), type: 'out'},

{ maker_id: 1, dt: ISODate('2015-07-04'), type: 'in'},

] }

Page 40: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{ _id: 1, name: 'scope', borrows: [

{ maker_id: 1, dt: ISODate('2015-06-12'), type: 'out'},

{ maker_id: 1, dt: ISODate('2015-07-04'), type: 'in'}, ] }

Embed?Ownership?

Work together?

Bound growth?

Lifetime?

Page 41: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: …,maker: { _id: 17, name: 'bob' }tool : { _id: 1, name: 'scope'},date: ISODate('2015-11-26 18:30Z'),action: 'out'

}

Check tool in / out

Ledger

Page 42: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Maker gets certified

Borrow & return tool

Report tool usage?

Page 43: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

db.toolLog.aggregate([{$match: {action: 'out', date: {$gte: ISODate('2015-09-01')

}}},{$group: {_id: "$tool.name", count: {$sum: 1}}},{$sort: {count: -1}}

]);

{ "_id" : "hammer", "count" : 2 }{ "_id" : "torch", "count" : 1 }

Page 44: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

db.toolLog.aggregate([{$match: {action: 'out', date: {$gte: ISODate('2015-09-01') }}},

{$group: {_id: "$maker.name", count: {$sum: 1}}},{$sort: {count: -1}}

]);

{ "_id" : "bob", "count" : 2 }{ "_id" : "kim", "count" : 1 }

Page 45: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{ "maker" : "kim", "has" : "hammer", "since" : ISODate("2015-10-03…"), "days" : 31

}

{ "maker" : "bob", "has" : "torch", "since" : ISODate("2015-09-03…"), "days" : 61

}

"Audit Report"

Page 46: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

db.toolLog.aggregate([{$sort: {date: -1}},{$group: {

_id: "$tool", action: {$first: "$action"}, at: {$first: "$date"}, by: {$first: "$maker"} }

},{$match: {action: "out"}},{$project: {

_id: 0,"maker": "$by.name", "has": "$_id.name",since: "$at",days: {$divide:[{$subtract: [{$literal: ISODate()}, "$at"]},

1000*60*60*24]}}}

]);

"Audit Report"

Page 47: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

{_id: …,maker: { _id: 17, name: 'bob' }tool : { _id: 123, name: 'hammer'},date: ISODate('2015-11-26 18:30Z'),action: 'check-out'

}

db.toolLog.createindex({date: 1,

"tool.name": 1})

Page 48: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Let the engine work for you

Page 49: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Toolasset_id UUIDtype_id int

Tool Typeid intname vharchar

Assetid UUIDname nvarchar(32)is_stationary bit

TableLand

Certificationperson_id UUIDasset_id UUIDname varchar(16)

Member Certificationmember_id UUIDcertification_id UUID

Personid UUIDfirst_name nvarchar(32)last_name nvarchar(32)

Maker

person_id UUID

status_id Int (not null)

Maker Statusid intstatus varchar(16)

Member Tool Logasset_id UUIDtype_id int

date datetime

Page 50: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

maker

tool

toolLog

{_id: 17,name: 'bob',status: 'paid',certified: [

{t:'cnc', d:'2015-11-26'}

],}

{_id: 2,name: 'drill',type: 'power',

}

{_id: 789234,maker: { _id: 17, name:

'bob' }tool : { _id: 2, name:

'drill'},date: ISODate('2015-11-26

18:30Z'),action: 'check-out'

}

Page 51: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Key interactions drivesdocument design.

Page 52: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Data that works together lives together.

Page 53: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Embed immutable data.

Page 54: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

Let the engine work for you

Page 55: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thinking Document

*Rules enforced by application.

Page 56: MongoDB Days Silicon Valley: From Story to Document: Applying MongoDB Thinking to Business Requirements

Thank you! Nuri Halperin +N Consulting [email protected] nurih @nurih