Tuesday, 13 January 2015

Designing Product Catalog (e-Commerce) in MongoDB

In this post we will discuss a real life use case of e-commerce specific domain. We will try to design a product catalog data model in MongoDB.

The basic entities of a product catalog building application can be depicted as follows:
- Product
- Product Attributes
- Price
- Ratings and Review
- Cart
- Inventory
- Store 

There can be many more, for our example we are considering the above only.

Following diagram depicts the relationships among the entities:



To start the design first we need to consider user requirements which will be accessed most frequently. In an e-commerce web site the most frequent functionality to be used are:

- Browse Product Detail Page by category
- Search products by name or keyword
- Sort by price (low to high)

As product be the main entity we would model the data around a product. The 1-to-1 relationship can be represented by embedding the child document inside the parent while the 1-to-n relationship can be represented as an embedded array of multiple sub-documents. A suggested design would be as follows:

{
  _id: ObjectId("54794c28d5bf15daee4cb42b"),
  partNumber: "12345678",

  type: "SA",
  category: "Electronics",
  path: "Electronics/iPad and Tablets/iPad",

  name: "Apple® iPad Air 32GB Wi-Fi - Silver/White (MD789LL/A)",
  description: "The iPad Air from Apple is 20 percent thinner than the 4th generation iPad and weighs just one pound, so it feels unbelievably light in your hand. It comes with a 9.7-inch Retina display, the powerful A7 chip with 64-bit architecture, ultrafast wireless, powerful apps, and up to 10 hours of battery life",
  .....
  priceInfo: [
   {
     type: "list",
     startDate: ISODate("2012-03-09T20:55:36Z"),
     endDate: ISODate("2012-03-31T20:55:36Z"),
     price: 10,
     unit: "USD"
   },
   ....
  ],
  ....
  carted: [],
  inventory: {
     totalQty: 500,
     lastUpdatedOn: null,
     store: []
  }
  details: {
    maxResolution: "2048 x 1536",
    displayFeatures: "Retina Display, Backlit LED",
    computerFeatures: "Touch Screen, Built-in Speaker, Integrated Microphone, Built-In Bluetooth",
    processorBrand: "Apple",
    ....
  },

  reviews: []
}


You can see the full data model in Git. Click here.  

The above data model can address atomic write operation issues. While browsing thru product info user can access one one single collections. Also write operations should be touch more than one collection.

To improve the read performance of the functionality for fetch product details by category we can create index on the category field as below.

db.product.ensureIndex({category : 1})

The search functionality (by name and detail) can be enhanced by creating text index on those fields as shown below:

db.product.ensureIndex({name : "text"})
db.product.ensureIndex({description : "text"})

The "Sort by Price" feature can be implemented by creating an index on the price field in a descending manner. This should be on the list price only. See below:

db.product.ensureIndex({"priceInfo.0" : -1})


The carted field can be used for add to cart functionality. We can see this in the next post.


<< Prev                                                                                     Next >>

No comments:

Post a Comment