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
- E-commerce storeCustomers, products and categories, orders with line items, and addresses.
- Multi-vendor marketplaceSellers with listings, buyer orders split per seller, payouts and reviews.
- Inventory & warehousesStock levels per warehouse, stock movements, suppliers and purchase orders.
- Point of saleStores, registers and staff ringing up sales with mixed payments.
- Restaurant orderingMenus with items and modifiers, and orders for dine-in or takeaway.
- Food deliveryCustomers order from restaurants; couriers pick up, deliver and get rated.
- Promotions & couponsCampaigns issuing coupon codes, with redemption tracking against orders.
- Product catalogue with variantsProducts with options (size, colour), variants, images and nested categories.
- Subscription boxMonthly boxes, what goes in each, subscribers and their shipments.
- AuctionsAuctions with lots, timed bids, watchers and winners.
- Orders, single table (DynamoDB)Customers, orders and order lines in one table keyed by PK and SK, with a GSI for orders by status.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.