Mongoose model

  1. Data model schema

    These models are responsible for creating, reading and deleting documents from the Mongo database. We need to create a schema first. The schema can be created manually using NestJS decorator or mongoose.


  2. Create schema with NestJS decorator

    Create the schema in coffee.entity.ts in the entities folder. The file structure is:

    1
    2
    3
    4
    5
    6
    7
    8
    |-- src
    |-- coffees
    |-- coffees.module.ts
    |-- coffees.controller.ts
    |-- coffees.service.ts
    |-- dto
    |-- entities
    |-- coffee.entity.ts
    1
    2
    3
    4
    5
    6
    7
    8
    import { Schema } from '@nestjs/mongoose';

    @Schema()
    export class Coffee extends Document{ //To inherit Document.
    name: string;
    brand: string;
    flavors: string[];
    } //This is incomplete, see next code block

    The @Schema() decorator maps our Coffee class to the MongoDB collection of the same name, but with an extra ā€˜sā€™ at the end. Mongo automatically makes all collections plural and lowercase by default.

    The id is not needed here because mongoose adds the _id attribute by default.


  1. Define properties: @Prop()

    Add the @Prop() decorator to all our properties.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import { Schema } from '@nestjs/mongoose';

    @Schema()
    export class Coffee extends Document { //To inherit Document.
    @Prop()
    name: string;

    @Prop()
    brand: string;

    @Prop([String]) //Because flavors is an array of strings, [String] should be passed in @Prop().
    flavors: string[];
    }

    export const CoffeeSchema = SchemaFactory.createForClass(Coffee); //output

  1. Introduce the mongoose model into the module file

    Add MongooseModule.forFeature() to the module file, passing in an Array of objects. We use forFeature() to register Mongoose in our submodule.

    1
    2
    3
    4
    5
    6
    7
    8
    import { MongooseModule } from '@nestjs/mongoose';

    @Module({ imports: [MongooseModule.forFeature([ //Note [], the element is an object.
    {
    name: Coffee.name, //Coffee.name is obtained from coffee.entity.ts
    schema: CoffeeSchema, // Also obtained from coffee.entity.ts, but the final output.
    }
    ])], controllers: [CoffeesController], providers: [CoffeesService] })

    coffee.name is just a way to get the function name from a JavaScript class, which in this case gives us a string.


  1. Common spelling

    Ref

    1
    2
    @Prop({ type: MongooseSchema.Types.ObjectId, ref: "Price" })
    price: Price;

    Do not repeat spelling

    1
    2
    @Prop({ unique: true })
    email: string;

    default value

    1
    2
    3
    4
    5
    @Prop({ default: false })
    isDeleted: boolean;

    @Prop({ type: Date, default: new Date() }) //The default is the current time
    createdAt: Date;

    Object notation

    1
    2
    3
    4
    5
    6
    @Prop({ type: Object, required: true })
    courtSize: {
    name: string;
    length: number;
    width: number;
    }

    Maximum and minimum writing

    1
    2
    @Prop({ required: true, minLength: 5, maxLength: 50 })
    name: string;

    Object array writing

    1
    2
    @Prop({ type: Array, required: true })
    colors: [{ name: string; value: string }];

Share