How to set up Unique Compound Index in NestJS (MongoDB) How to set up Unique Compound Index in NestJS (MongoDB) mongoose mongoose

How to set up Unique Compound Index in NestJS (MongoDB)


Looks like its not possible to achieve it solely by use of the @nestjs/mongoose decorators, however its possible to declare index by use of SchemaFactory

@Schema()export class User {    @Prop()    groupId: string;    @Prop()    firstName!: string;    ...}export const UserSchema = SchemaFactory.createForClass(User);UserSchema.index({ groupId: 1, firstName: 1 }, { unique: true });

Then you must do either create migration to create this index or enable auto-index feature

@Schema({  autoIndex: true, // <--})export class User {    @Prop()    groupId: string;    @Prop()    firstName!: string;    ...}