
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
Now that we have Mongoose set up, we can start building our schema. The schema defines the structure of the documents in our database. In this lesson, we'll create a schema for a simple e-commerce application that includes products, reviews, and images.
Before diving into the code, it's essential to take a step back and analyze the requirements of our application. Let's break down the ideas and figure out how to structure our schema.
You can follow 3 steps to help you write the schema:
If you take a look through our application, you see it revolves around two main entities:
Here's what we know:
have a name, description, price, and a list of associated images. are associated with a product and contain an author, rating, and content.
To design an effective schema, we need to identify these entities, or , their attributes, and the relationships between them.
Next, let's define the attributes for each collection:
: The name of the product. (a String)
: The price of the product. (a Number)
: A description of the product. (a String)
: The category of the product. (a String)
: The URLs of the product images. (a String)
: The name and email of the reviewer. (an object)
: The rating given by the reviewer. (an Integer)
: The content of the review. (a String)
Because MongoDB is a document database, we can store related data together in a single document. This can help improve performance by reducing the number of queries needed to retrieve data.
For instance, we're storing the author's name and email together in the review document. This way, we can retrieve the author's information along with the review without needing to perform an additional query.
We'll use an embedded document, or to store the author's information in the review document.
Check this section for a review on Nested SchemasNow, let's determine the relationships between the entities:
can have multiple , and a review belongs to a single product. - This is a relationship between Product and Review.
This means we can use a to connect the two entities. We'll add a field to the Review collection to store the reference to the product it belongs to.
When designing the schema, it can be helpful to write the attributes and relationships down on paper or a whiteboard. This can help you visualize the structure of your database and ensure you haven't missed anything.
Let's create our schemas and models.
First, let's create a home for these files.
Create a new directory called in the directory and add two files:
Next, we can begin our process of building the schema:
product.ts
//
import { Schema, model, Document, models } from "mongoose";
interface Product {
}
const ProductSchema = new Schema<Product>({
});
const Product = models?.Product || model<Product>("Product", ProductSchema);
export default Product;
review.ts
//
import { Schema, model, Document, models } from "mongoose";
interface Review {
}
const ReviewSchema = new Schema<Review>({
});
const Review = models?.Review || model<Review>("Review", ReviewSchema);
export default Review;
product.ts
//
import { Schema, model, Document, models } from "mongoose";
interface Product {
name: string;
price: number;
description: string;
category: string;
images: string[];
}
const ProductSchema = new Schema<Product>({
name: { type: String, required: true },
price: { type: Number, required: true },
description: { type: String, required: true },
category: { type: String, required: true },
images: [{ type: String, required: true }],
});
const Product = models?.Product || model<Product>("Product", ProductSchema);
export default Product;
review.ts
//
const AuthorSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
});
interface Review {
author: {
name: string;
email: string;
};
rating: number;
content: string;
}
const ReviewSchema = new Schema<Review>({
author: { type: AuthorSchema, required: true },
rating: { type: Number, required: true },
content: { type: String, required: true },
});
const Review = models?.Review || model<Review>("Review", ReviewSchema);
export default Review;
review.ts
//
import { Schema, model, Document, models } from "mongoose";
const AuthorSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
});
interface Review {
author: {
name: string;
email: string;
};
rating: number;
content: string;
productId: string | Schema.Types.ObjectId;
}
const ReviewSchema = new Schema<Review>({
author: { type: AuthorSchema, required: true },
rating: { type: Number, required: true },
content: { type: String, required: true },
// connect to Product
productId: { type: Schema.Types.ObjectId, ref: "Product", required: true },
});
const Review = models?.Review || model<Review>("Review", ReviewSchema);
export default Review;
In this lesson, we analyzed our application requirements, defined the schema for our database, and established the relationships between the collections.
We've build our schema for the products and reviews collections, using references and nested schemas to connect the entities.
In the next lesson, we'll generate our begin writing our CRUD operations.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.