How Can You Use MongoDB for E-Commerce?

mongodb e-commerce

MongoDB is a NoSQL database that stores data in JSON-like documents with dynamic schemas. It simplifies data integration and offers better scalability than traditional relational databases.

Today’s e-commerce sites need rich data models and dynamic queries. MongoDB provides this, making it a popular choice for many companies.

There’s a wide range of features you can build out for your store using MongoDB. In this post, we examine a few important areas that you can model: product catalogs, shopping carts, and payments.

E-Commerce Data Models Using MongoDB

Product Catalogs

The product catalog is a record of all the items available for sale on the e-commerce website. The product data model usually includes the following:

  • unique identifier (SKU)
  • price
  • title
  • description
  • quantity
  • Image

Below is an example of a command using the product document:

db.inventory.insertOne( { 
    item: "journal", 
    price: 9.99, 
    qty: 25, 
    size: { h: 14, l: 21, w: 1 }, 
    features: "Beautiful, handmade journal.",
    categories: ["writing", "bestseller"],
    image: "items/journal.jpg"  
} )

MongoDB aims to structure schema in a way that your application can return all necessary data in a single query. If you want to add complexity to your MongoDB model, you can add embeddable documents to a record.

 

Shopping Cart

The e-commerce shopping cart gives users the ability to select and reserve items from the product catalog. These products are held in the cart until they complete the checkout process.

It is important to ensure that the cart doesn’t allow customers to hold more items than are available in your inventory. The cart should also release any items back to your inventory when a user abandons their cart. Here is an example of a document modeling the cart:

{
   _id: "the_users_session_id",
   status:'active'

   quantity: 3,
   total: 575,

   products: []
 }

The specific items the customer adds to their cart will appear in the products array. You can create the cart with the following insert() operation:

db.carts.insert({
   _id: "the_users_session_id",
   status:'active',
   quantity: 3,
   total: 575,
   products: []});

After adding items to the cart, you can check to make sure there is enough inventory available with the following:

db.products.update({
     sku: "154B", quantity: {$gte: 1}
   }, {
     $inc: {quantity: -1},
     $push: {
       in_carts: {
         quantity:1, id: "the_users_session_id", timestamp: new ISODate()
       }
     }
   })

The operation only works when there is enough inventory. You can use getLastError to see the results of the attempted update:

 

Payments

Payment applications allow you to collect billing details from your customers. When modeling this part of your e-commerce system, security is of utmost importance. The PCI DSS guidelines make it clear that you should not store customers’ card data unless it’s absolutely necessary. You should also never store authentication data such as verification codes.

You can protect cardholder information by encrypting the data. MongoDB allows you to encrypt data files and perform automatic client-side encryption. Another option is to only store the four digits of the card.

In this situation, you are not required to encrypt your data. However, encryption is still required if you store other personally identifiable information. Here is an example of a model with just the last four card digits:

db.payments.remove( { } )
db.payments.insertOne( { 
    customerId: "journalfanatic@e/mail.com",
    status: "verified",
    gateway: "stripe",
    type: "credit",
    amount: NumberDecimal(1.00),
    card: {
        brand: "Visa",
        panLastFour: "4242",
        expirationMonth: 1,
        expirationYear: 2090,
        cvvVerified: true
    }
} )

Some payment processors will provide tokenization services to enable easier PCI compliance. With tokenization, all payment information is replaced with a token string. Your e-commerce systems can then easily use these strings to track a payment. This can help simplify your code as all PCI requirements are passed off to your processor. Below you can see an example of a tokenized model:

db.payments.insertOne( { 
	customerId: "journalfanatic@e/mail.com",
	status: "awaitingVerification",
	gateway: "stripe",
	type: "token",
	token: "card_1IDHBZFdjJUqVVV2gPlbz8BC"
} )

Building NoSQL Data Models for E-Commerce

MongoDB’s rich document capabilities allow it to model an almost endless variety of applications. If you want to learn more about how you can use MongoDB for e-commerce, check out this detailed post on building a NoSQL e-Commerce data model.


Topics: Commerce
Dana Lim

Content marketer @ fabric. Previously marketing @ KHON-TV and Paramount Pictures.

Learn more about fabric