From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Creating a schema

Creating a schema

Now, let's see how to create a schema for the collection. I'll create a folder inside the SRC, naming it as products, a TypeScript file, product.schema.ts. Now inside this file, I'll create a class called product. And to mark it as a schema, I'll give the schema decorator to the class. So by marking the class with the schema decorator, it lets NestJS know that this class defines the schema for the MongoDB collection. Talking about the collection, if we want to set the collection name explicitly rather than relying on the Mongoose driver to automate the name, we can specify the collection property and inside the schema decorator and assign a string value. I'll keep the collection name as products. Now let's specify the properties that is fields inside the collection. I'll give the property product name of type string. To mark this as property inside the collection, I'll give the property decorator, which is from the nestjs mongoose package. So this is going to add the product name…

Contents