Slide 1

Slide 1 text

MongoDB for eCommerce Bulat Shakirzyanov http://goo.gl/pXWnC Wednesday, February 15, 12

Slide 2

Slide 2 text

Hello, world! • @avalanche123 • github.com/avalanche123 • avalanche123.com Wednesday, February 15, 12

Slide 3

Slide 3 text

eCommerce is hard Wednesday, February 15, 12

Slide 4

Slide 4 text

Wednesday, February 15, 12

Slide 5

Slide 5 text

an ecommerce platform Wednesday, February 15, 12

Slide 6

Slide 6 text

unlimited sellers, suppliers and shoppers Wednesday, February 15, 12

Slide 7

Slide 7 text

Wednesday, February 15, 12

Slide 8

Slide 8 text

unlimited product categories Wednesday, February 15, 12

Slide 9

Slide 9 text

Apparel Electronics Jewelry Books Appliances Tools Wednesday, February 15, 12

Slide 10

Slide 10 text

and 4703 more categories Wednesday, February 15, 12

Slide 11

Slide 11 text

different product attributes Wednesday, February 15, 12

Slide 12

Slide 12 text

• size • style • color • brand • megapixels • image stabilization • memory slot type • battery life Wednesday, February 15, 12

Slide 13

Slide 13 text

how do you model that? Wednesday, February 15, 12

Slide 14

Slide 14 text

EAV Entity Attribute Value Wednesday, February 15, 12

Slide 15

Slide 15 text

Entity Entity PK id type Attribute Attribute PK id name type Value PK id FK entity_id FK attribute_id value Wednesday, February 15, 12

Slide 16

Slide 16 text

id type author size 1 book Charles Dickens NULL 2 shirt NULL m Products Wednesday, February 15, 12

Slide 17

Slide 17 text

id type 1 book 2 shirt id name type 1 author string 2 size enum(‘s’,‘m’,‘l’) Entities Attributes id entity_id attribute_id value 1 1 1 Charles Dickens 2 2 2 m Values Wednesday, February 15, 12

Slide 18

Slide 18 text

SELECT `e`.`id` as id, `v`.`value` as author FROM `entities` e JOIN `values` v ON `v`.`entity_id` = `e`.`id` JOIN `attributes` a ON `v`.`attribute_id` = `a`.`id` WHERE `e`.`type`='book' AND `a`.`name` = 'author' id author 1 Charles Dickens Wednesday, February 15, 12

Slide 19

Slide 19 text

problem solved? Wednesday, February 15, 12

Slide 20

Slide 20 text

Configurable products Wednesday, February 15, 12

Slide 21

Slide 21 text

Product Option Product Option FK product_id FK option_id Entity Entity PK id type Attribute Attribute PK id name type Value PK id FK entity_id FK attribute_id value Wednesday, February 15, 12

Slide 22

Slide 22 text

id type 1 product 2 product 3 option id name type 1 author string 2 size enum(‘s’, ‘m’, ‘l’) 3 entry_type string 4 display_type string Entities Attributes product_id option_id 2 3 Product Options Wednesday, February 15, 12

Slide 23

Slide 23 text

id entity_id attribute_id value 1 1 1 Charles Dickens 5 1 3 book 6 2 3 shirt 7 1 4 simple 8 2 4 configurable 9 3 2 m 10 3 3 shirt 11 3 4 simple Values Wednesday, February 15, 12

Slide 24

Slide 24 text

SELECT `e`.`id` as id, `v1`.`value` as type `v2`.`value` as display_type FROM `entities` e JOIN `values` v1 ON `v1`.`entity_id` = `e`.`id` JOIN `attributes` a1 ON `v1`.`attribute_id` = `a1`.`id` JOIN `values` v2 ON `v2`.`entity_id` = `e`.`id` JOIN `attributes` a2 ON `v2`.`attribute_id` = `a2`.`id` WHERE `e`.`id` = 2 AND `a1`.`name` = 'type' AND `a2`.`name` = 'display_type' id type display_type 2 shirt configurable Wednesday, February 15, 12

Slide 25

Slide 25 text

SELECT `o`.`option_id` as id, `v1`.`value` as type `v2`.`value` as display_type `v3`.`value` as size FROM `options` o JOIN `entities` e ON `e`.`id` = `o`.`option_id` JOIN `values` v1 ON `v1`.`entity_id` = `o`.`option_id` JOIN `attributes` a1 ON `v1`.`attribute_id` = `a1`.`id` JOIN `values` v2 ON `v2`.`entity_id` = `o`.`option_id` JOIN `attributes` a2 ON `v2`.`attribute_id` = `a2`.`id` JOIN `values` v3 ON `v3`.`entity_id` = `o`.`option_id` JOIN `attributes` a3 ON `v3`.`attribute_id` = `a3`.`id` WHERE `o`.`product_id`=2 AND `a1`.`name` = 'type' AND `a2`.`name` = 'display_type' AND `a3`.`name` = 'size' id type display_type size 3 shirt simple m Wednesday, February 15, 12

Slide 26

Slide 26 text

more fun... Wednesday, February 15, 12

Slide 27

Slide 27 text

Grouped products Wednesday, February 15, 12

Slide 28

Slide 28 text

Product Group Product Group FK group_id FK product_id Product Option Product Option FK product_id FK option_id Entity Entity PK id type Attribute Attribute PK id name type Value PK id FK entity_id FK attribute_id value Wednesday, February 15, 12

Slide 29

Slide 29 text

id type 1 product 2 product 3 option 4 group 5 product 6 product id name type 1 author string 2 size enum(‘s’, ‘m’, ‘l’) 3 entry_type string 4 display_type string Entities Attributes product_id option_id 2 3 Product Options group_id product_id 4 5 4 6 Product Groups Wednesday, February 15, 12

Slide 30

Slide 30 text

id entity_id attribute_id value 1 1 1 Charles Dickens 10 4 3 knife 11 4 4 group 12 5 3 knife 13 5 4 simple 14 6 3 knife 15 6 4 simple Values Wednesday, February 15, 12

Slide 31

Slide 31 text

Almost done Wednesday, February 15, 12

Slide 32

Slide 32 text

Bundled product Wednesday, February 15, 12

Slide 33

Slide 33 text

six months later... Wednesday, February 15, 12

Slide 34

Slide 34 text

Wednesday, February 15, 12

Slide 35

Slide 35 text

schema that can store entries of any structure Wednesday, February 15, 12

Slide 36

Slide 36 text

wrong use of tables Wednesday, February 15, 12

Slide 37

Slide 37 text

Wednesday, February 15, 12

Slide 38

Slide 38 text

alternative Wednesday, February 15, 12

Slide 39

Slide 39 text

& Wednesday, February 15, 12

Slide 40

Slide 40 text

use document-store for unstructured data and RDBMS for structured, highly relational data Wednesday, February 15, 12

Slide 41

Slide 41 text

{ "_id": ObjectId("4bffd798fdc2120019040000") , "type": "simple" , "name": "Small T-Shirt" , "price": 12.99 , "SKU": "TSHRT-1S" , "size": "S" , "brand": "Some cool brand" } Attributes Type Wednesday, February 15, 12

Slide 42

Slide 42 text

{ "_id": ObjectId("4bffd798fdc2120019040000") , "name": "T-Shirt" , "type": "configurable" , "options": [ { "name": "small" , "price": 12.99 , "SKU": "TSHRT-1S" , "size": "S" } , { "name": "medium" , "price": 15.99 , "SKU": "TSHRT-1M" , "size": "M" } , { "name": "large" , "price": 17.99 , "SKU": "TSHRT-1L" , "size": "L" } ] , "brand": "Some cool brand" } Attributes Type Wednesday, February 15, 12

Slide 43

Slide 43 text

{ "_id": ObjectId("4bffd798fdc2120019040000") , "name": "Chef's Knife" , "type": "grouped" , "products": [ { "name": "5\"" , "price": 29.95 , "SKU": "CHF-KNF5" , "length": "5\"" } , { "name": "6\"" , "price": 34.95 , "SKU": "CHF-KNF6" , "length": "6\"" } , { "name": "7\"" , "price": 39.95 , "SKU": "CHF-KNF7" , "length": "7\"" } ] } Attributes Type Wednesday, February 15, 12

Slide 44

Slide 44 text

Benefits Wednesday, February 15, 12

Slide 45

Slide 45 text

Performance Wednesday, February 15, 12

Slide 46

Slide 46 text

Flexibility Wednesday, February 15, 12

Slide 47

Slide 47 text

Drawbacks Wednesday, February 15, 12

Slide 48

Slide 48 text

Drawbacks • No relations and cascades • No structure can lead to chaos • Unique indexes index NULL values • Learning curve Wednesday, February 15, 12

Slide 49

Slide 49 text

MongoDB ODM We’ve fixed it http://www.doctrine-project.org/projects/mongodb_odm Wednesday, February 15, 12

Slide 50

Slide 50 text

MongoDB ODM • Relations and cascades • Structure through classes • In-memory change tracking and optimized single flush persistence • On-the-fly migrations • API identical to Doctrine ORM Wednesday, February 15, 12

Slide 51

Slide 51 text

References • Oil Drilling Platform in the Santa Barbara CA Channel - http://www.flickr.com/photos/ mikebaird/3898808431/ • Busy times on Main Street - http://www.flickr.com/photos/yourfavoritemartian/4867673963/ • Black Vector T-Shirt - http://skyje.com/gallery/black-vector-t-shirt/ • Black Vector T-Shirt - http://skyje.com/gallery/black-vector-t-shirt/ Wednesday, February 15, 12

Slide 52

Slide 52 text

Questions? Thank you! Wednesday, February 15, 12

Slide 53

Slide 53 text

come work for Wednesday, February 15, 12