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.

Create and save product to MongoDB

Create and save product to MongoDB

Alright, so we have registered the product schema inside the product module. Now we are going to perform the CRUD functionalities. So let's create the product service file. I'll give the command nest generate service products slash product and no spec. Inside the service file, we first have to inject the product model that is schema. So I'll give the constructor and use the inject model decorator provided by the nestjs mongoose package. It is specifically used for injecting mongoose models into the service files. So let's give the model name that is product model dot name. Now you might think that why do we have to use the dot name property everywhere with the product schema. Well, this is basically how the nest.js and mongoose manages the models and schema names. The name property of the class product holds the string representation of the class name, which is what mongoose uses to register and reference the model. So using product.name ensures that the string name used to register…

Contents