Simple e-commerce schemaChristian Kvalheim @christkv
Agenda• The product• The categories• Product by categories• Category navigation• The cart• Add product to cart• Remove pro...
The product {     sku: "111445GB3",     title: "Simsong One mobile phone",     description: "The greatest Onedroid phone o...
The product, add categoriesdb.products.update(  {sku: "111445GB3"},  {$set: { categories: [mobile/15G, mobile/fm] }});  { ...
The product, add categories• Add an index on the categoriesdb.products.ensureIndex({categories:1 })• Locate all the produc...
Categories  {      title: "Mobiles containing a FM radio",      parent: "mobile",      path: "mobile/fm"  }db.categories.i...
Categories, queries• Locate all categories directly under /mobiledb.categories.find({parent: /^mobile/}, {_id: 0, path: 1}...
Adding to Cart
Cart, example schema{    _id: "the_users_session_id",    status:active    quantity: 2,    total: 2000,    products: []}• A...
Cart, add item to cart    db.carts.update({        _id: "the_users_session_id", status:active      }, {        $set: { mod...
Cart, add to cart, inventory cover  db.products.update({      sku: "111445GB3", quantity: {$gte: 1}    }, {      $inc: {qu...
Update Cart
Cart, update quantity  db.carts.update({       _id: "the_users_session_id", "products.sku": "111445GB3", status:  "active"...
Cart, update inventory  db.products.update({       sku: "111445GB3",       "in_carts.id": "the_users_session_id",       qu...
Cart, on failure rollback  if(!db.runCommand({getLastError:1}).updatedExisting) {    db.carts.update({         _id: "the_u...
Expire Carts
Cart, expire all the carts var carts = db.carts.find({status:"expiring"}) for(var i = 0; i < carts.length; i++) {   var ca...
Checkout
Insert an Order db.orders.insert({    created_on: new ISODate("2012-05-17T08:14:15.656Z"),    shipping: {       customer: ...
Cart, finish up and clean updb.carts.update({    _id: "the_users_session_id"  }, {    $set: {status:"complete"}  });• Set t...
Thank you and happy     schemaing  twitter: @christkv
Upcoming SlideShare
Loading in …5
×

Mongo db ecommerce

3,156 views

Published on

A simple schema design set for modeling a product, category and cart for an eCommerce system using mongodb

Published in: Technology, Business
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,156
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
55
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Mongo db ecommerce

    1. 1. Simple e-commerce schemaChristian Kvalheim @christkv
    2. 2. Agenda• The product• The categories• Product by categories• Category navigation• The cart• Add product to cart• Remove product from cart• Expire cart• The checkout
    3. 3. The product { sku: "111445GB3", title: "Simsong One mobile phone", description: "The greatest Onedroid phone on the market .....", manufacture_details: { model_number: "A123X", release_date: new ISODate("2012-05-17T08:14:15.656Z") }, shipping_details: { weight: 350, width: 10, height: 10, depth: 1 }, quantity: 99, pricing: { price: 1000 } }
    4. 4. The product, add categoriesdb.products.update( {sku: "111445GB3"}, {$set: { categories: [mobile/15G, mobile/fm] }}); { sku: "111445GB3", title: "Simsong One mobile phone", description: "The greatest Onedroid phone on the market .....", manufacture_details: { model_number: "A123X", release_date: new ISODate("2012-05-17T08:14:15.656Z") }, shipping_details: { weight: 350, width: 10, height: 10, depth: 1 }, quantity: 99, categories: [mobile/15G, mobile/ fm],
    5. 5. The product, add categories• Add an index on the categoriesdb.products.ensureIndex({categories:1 })• Locate all the products in a categorydb.products.find({categories: /^mobile/fm/})• If we use case sensitive pre-fix regular expressions we use the btree index and have very fast lookups
    6. 6. Categories { title: "Mobiles containing a FM radio", parent: "mobile", path: "mobile/fm" }db.categories.insert({title: "Mobiles containing a FM radio", parent:"mobile", path: "mobile/fm"})db.categories.insert({title: "Mobiles with 15G support", parent:"mobile", path: "mobile/15G"})db.categories.ensureIndex({parent: 1, path: 1})db.categories.ensureIndex({path: 1})
    7. 7. Categories, queries• Locate all categories directly under /mobiledb.categories.find({parent: /^mobile/}, {_id: 0, path: 1})• Locate all the categories under /mobiledb.categories.find({path: /^mobile/}, {_id: 0, path: 1})
    8. 8. Adding to Cart
    9. 9. Cart, example schema{ _id: "the_users_session_id", status:active quantity: 2, total: 2000, products: []}• Add item to cart• Update inventory only if we have enough quantity• Rollback if inventory update fails
    10. 10. Cart, add item to cart db.carts.update({ _id: "the_users_session_id", status:active }, { $set: { modified_on: ISODate() }, $push: { products: { sku: "111445GB3", quantity: 1, title: "Simsong One mobile phone", price:1000 } } });{ _id: "the_users_session_id", status:active quantity: 2, total: 2000, products: [{sku: "111445GB3", quantity: 1, title: "Simsong One mobilephone", price:1000}]}
    11. 11. Cart, add to cart, inventory cover db.products.update({ sku: "111445GB3", quantity: {$gte: 1} }, { $inc: {quantity: -1}, $push: { in_carts: { quantity:1, id: "the_users_session_id", timestamp: new ISODate() } }• If we did not update the stock, roll back cart addif(!db.runCommand({getLastError:1}).updatedExisting) { db.carts.update({ _id: "the_users_session_id" }, { $pull: {products: {sku:"111445GB3"}} })}
    12. 12. Update Cart
    13. 13. Cart, update quantity db.carts.update({ _id: "the_users_session_id", "products.sku": "111445GB3", status: "active" }, { $set: { modified_on: new ISODate(), "products.$.qty": new_quantity } })• Update the quantity in the cart • new_quantity = 2 • old_quantity = 1 • delta_quantity = new_quantity - old_quantity
    14. 14. Cart, update inventory db.products.update({ sku: "111445GB3", "in_carts.id": "the_users_session_id", quantity: { $gte: 1 } }, { $inc: { quantity: (-1)*delta_quantity }, $set: { "in_carts.$.quantity": new_quantity, timestamp: new ISODate() } })• Attempt to update the inventory • new_quantity = 2 • old_quantity = 1 • delta_quantity = new_quantity - old_quantity
    15. 15. Cart, on failure rollback if(!db.runCommand({getLastError:1}).updatedExisting) { db.carts.update({ _id: "the_users_session_id", "products.sku": "111445GB3" }, { $set : { "in_carts.$.quantity": old_quantity} }) }• If the update fails roll back the cart to the previous quantity • new_quantity = 2 • old_quantity = 1 • delta_quantity = new_quantity - old_quantity
    16. 16. Expire Carts
    17. 17. Cart, expire all the carts var carts = db.carts.find({status:"expiring"}) for(var i = 0; i < carts.length; i++) { var cart = carts[i] for(var j = 0; j < cart.products.length; j++) { var product = cart.products[i] db.products.update({ sku: product.sku, "in_carts.id": cart._id, "in_carts.quantity": product.quantity }, { $inc: {quantity: item.quantity}, $pull: {in_carts: {id: cart._id}} }) } db.carts.update({ _id: cart._id, $set: {status: expired} }) }
    18. 18. Checkout
    19. 19. Insert an Order db.orders.insert({ created_on: new ISODate("2012-05-17T08:14:15.656Z"), shipping: { customer: "Peter P Peterson", address: "Longroad 1343", city: "Peterburg", region: "", state: "PE", country: "Peteonia", delivery_notes: "Leave at the gate", tracking: { company: "ups", tracking_number: "22122X211SD", status: "ontruck", estimated_delivery: new ISODate("2012-05-17T08:14:15.656Z") }, }, payment: { method: "visa", transaction_id: "2312213312XXXTD" } products: { {quantity: 2, sku:"111445GB3", title: "Simsong mobile phone", unit_cost:1000, currency:"USD"} } })
    20. 20. Cart, finish up and clean updb.carts.update({ _id: "the_users_session_id" }, { $set: {status:"complete"} });• Set the cart status as completeddb.products.update({ "in_carts.id": "the_users_session_id" }, { $pull: {in_carts: {id: "the_users_session_id"}} }, false, true);• Remove all carts from products
    21. 21. Thank you and happy schemaing twitter: @christkv

    ×