You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
3 Hrs
21 Min
6 Seg

Implementando Mongoose en Módulos

11/24
Resources

In addition to making the connection to the database, Mongoose allows the mapping of information to standardize its structure in each MongoDB collection.

Creating entities with Mongoose

Creating entities to shape your data is a simple task thanks to Mongoose.

Step 1: creating the entity and its properties

Assuming you need a collection in Mongoose to store products, start by creating a file called product.entity.ts in the products module of your application.

// modules/products/product.entity.tsimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';import { Document } from 'mongoose'; @Schema()export class Product extends Document { @Prop({ required: true }) name: string; @Prop() description: string; @Prop({ type: Number }) price: number; @Prop({ type: Number }) stock: number; @Prop() image: string; }export const ProductSchema = SchemaFactory.createForClass(Product);

Observe the @Prop() decorator to map each attribute of the Product class that extends Document and tell Mongoose that it is a document property. Exporting ProductSchema that, thanks to SchemaFactory that is responsible for creating and performing the data mapping, you will be able to perform the subsequent queries from the services.

Step 2: entity import

Now you just have to import the entity into the module it belongs to as follows:

// modules/products/products.module.tsimport { MongooseModule } from '@nestjs/mongoose';import { Product, ProductSchema } from './entities/product.entity'; @Module({ imports: [ MongooseModule.forFeature([ { name: Product.name, schema: ProductSchema, }, }, ]) ] })export class ProductsModule {}

Import MongooseModule and you have to assign a name to the collection and inject the schema it will use.

This way, you will be creating the products collection in your MongoDB database and you already have mapped in your application the structure of each document it will contain to avoid errors.


Contribution created by: Kevin Fiorentino.

Example code to implement Mongoose

// src/products/entities/product.entity.tsimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';import { Document } from 'mongoose'; @Schema()export class Product extends Document { @Prop({ required: true }) name: string; @Prop() description: string; @Prop({ type: Number }) price: number; @Prop({ type: Number }) stock: number; @Prop() image: string; }export const ProductSchema = SchemaFactory.createForClass(Product);
// src/products/products.module.ts...import { MongooseModule } from '@nestjs/mongoose';import { Product, ProductSchema } from './entities/product.entity'; @Module({ imports: [ MongooseModule.forFeature([ { name: Product.name, schema: ProductSchema, }, }, ]), ], ... })export class ProductsModule {}

Contributions 3

Questions 4

Sort by:

Want to see more contributions, questions and answers from the community?

Si presentan este error “0 , rxjs_1.lastValueFrom) is not a function” Podría solucionarse con una de estas opciones.
npm i rxjs@^7
yarn add rxjs@^7
pnpm i rxjs @^7

👍

👏