Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB and Nest.js: Define a custom name for a collection

I have a schema like this:

    @Schema()
    export class Pais extends Document {
      @Prop(
        raw({
          codigo: { type: String, index: true, unique: true },
        }),
      )
      @Prop()
      descripcion: string;
    }
    
    export const PaisSchema = SchemaFactory.createForClass(Pais);
    
    PaisSchema.plugin(uniqueValidator, { message: `{PATH} debe ser único` });

By default nest.js add an 's' to the class name, so it would be 'paiss' for the collection, but I want the name to be 'paises'.

On the module I tried this:

    @Module({
      imports: [
        MongooseModule.forFeature([{ name: 'paises', schema: PaisSchema }]),
      ],

but it didn't work. How can I solve this problem?

like image 936
Alex Avatar asked Aug 03 '20 18:08

Alex


1 Answers

i found a solution, just had to set the @Schema() decorator like this

@Schema({ collection: 'paises' })
like image 148
Alex Avatar answered Nov 14 '22 22:11

Alex