Templates/Commerce

Product catalogue (MongoDB) database schema

Products with embedded variants and images, categories and reviews that reference them.

3 collections, 3 references

Opens as a new diagram you can change. Free.

Collections

categories

A tree of categories, each pointing at its parent.

  • _idobjectId not null
  • namestring not null
  • slugstring not null
  • parentId> categoriesobjectId

products

What's for sale; variants and images live inside the product, read with it.

  • _idobjectId not null
  • namestring not null
  • slugstring not null
  • descriptionstring
  • categoryId> categoriesobjectId not null
  • pricedecimal not null
  • variantsarray not null
  • variants.skustring not null
  • variants.sizestring
  • variants.colourstring
  • variants.stockint not null
  • imagesarray
  • images.urlstring not null
  • images.altstring
  • tagsarray
  • createdAtdate not null

reviews

Reviews grow without bound, so they reference the product rather than live inside it.

  • _idobjectId not null
  • productId> productsobjectId not null
  • authorobject not null
  • author.namestring not null
  • author.userIdobjectId
  • ratingint not null
  • bodystring
  • createdAtdate not null

As mongosh script (collections, validators, indexes)

The diagram also exports JSON Schema per collection, Mongoose models, Prisma (MongoDB), Sample documents (mongosh), Sample documents (mongoimport, Extended JSON).

// Product catalogue (MongoDB): MongoDB collections, validators and indexes.
// Run with: mongosh "<connection string>" schema.js
// References the application keeps (not enforced by MongoDB):
//   categories.parentId → categories._id
//   products.categoryId → categories._id
//   reviews.productId → products._id

// categories
db.createCollection("categories", {
  validator: { $jsonSchema: {
    "title": "categories",
    "description": "A tree of categories, each pointing at its parent.",
    "bsonType": "object",
    "required": [
      "_id",
      "name",
      "slug"
    ],
    "properties": {
      "_id": {
        "bsonType": "objectId"
      },
      "name": {
        "bsonType": "string"
      },
      "slug": {
        "bsonType": "string"
      },
      "parentId": {
        "bsonType": "objectId"
      }
    }
  } },
});
db.getCollection("categories").createIndex({
  "slug": 1
}, {
  "name": "slug_1",
  "unique": true
});

// products
db.createCollection("products", {
  validator: { $jsonSchema: {
    "title": "products",
    "description": "What's for sale; variants and images live inside the product, read with it.",
    "bsonType": "object",
    "required": [
      "_id",
      "name",
      "slug",
      "categoryId",
      "price",
      "variants",
      "createdAt"
    ],
    "properties": {
      "_id": {
        "bsonType": "objectId"
      },
      "name": {
        "bsonType": "string"
      },
      "slug": {
        "bsonType": "string"
      },
      "description": {
        "bsonType": "string"
      },
      "categoryId": {
        "bsonType": "objectId"
      },
      "price": {
        "bsonType": "decimal"
      },
      "variants": {
        "bsonType": "array",
        "items": {
          "bsonType": "object",
          "required": [
            "sku",
            "stock"
          ],
          "properties": {
            "sku": {
              "bsonType": "string"
            },
            "size": {
              "bsonType": "string"
            },
            "colour": {
              "bsonType": "string"
            },
            "stock": {
              "bsonType": "int"
            }
          }
        }
      },
      "images": {
        "bsonType": "array",
        "items": {
          "bsonType": "object",
          "required": [
            "url"
          ],
          "properties": {
            "url": {
              "bsonType": "string"
            },
            "alt": {
              "bsonType": "string"
            }
          }
        }
      },
      "tags": {
        "bsonType": "array"
      },
      "createdAt": {
        "bsonType": "date"
      }
    }
  } },
});
db.getCollection("products").createIndex({
  "slug": 1
}, {
  "name": "slug_1",
  "unique": true
});
db.getCollection("products").createIndex({
  "categoryId": 1,
  "price": 1
}, {
  "name": "categoryId_1_price_1"
});
db.getCollection("products").createIndex({
  "variants.sku": 1
}, {
  "name": "variants.sku_1",
  "unique": true
});
db.getCollection("products").createIndex({
  "name": "text",
  "description": "text"
}, {
  "name": "name_text_description_text"
});

// reviews
db.createCollection("reviews", {
  validator: { $jsonSchema: {
    "title": "reviews",
    "description": "Reviews grow without bound, so they reference the product rather than live inside it.",
    "bsonType": "object",
    "required": [
      "_id",
      "productId",
      "author",
      "rating",
      "createdAt"
    ],
    "properties": {
      "_id": {
        "bsonType": "objectId"
      },
      "productId": {
        "bsonType": "objectId"
      },
      "author": {
        "bsonType": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "bsonType": "string"
          },
          "userId": {
            "bsonType": "objectId"
          }
        }
      },
      "rating": {
        "bsonType": "int"
      },
      "body": {
        "bsonType": "string"
      },
      "createdAt": {
        "bsonType": "date"
      }
    }
  } },
});
db.getCollection("reviews").createIndex({
  "productId": 1,
  "createdAt": -1
}, {
  "name": "productId_1_createdAt_-1"
});

More Commerce templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.