NestJs index field with mongoose

Viewed 530

I have a question about how to index fields in Mongoose collections on NestJS. Is the indexing method done correctly in the following example? Do I have to set the autoindex field value to false at the schema level and then true the index value at the field level to be indexed?

@Schema({ timestamps: true, autoIndex: false })
export class Ticket {
  @Prop({ required: true, index: true })
  title: string;

  @Prop({ required: true, index: true })
  name: string;

  @Prop({ type: ContactModelSchema })
  contact: ContactModel;

  @Prop({ type: ContentModelSchema, index: true })
  acceptedContent: ContentModel;
}

export const TicketSchema = SchemaFactory.createForClass(Ticket);

@Schema({ timestamps: true, _id: false, autoIndex: false })
export class ContentModel {
  @Prop()
  websiteUrl: string;

  @Prop({ index: true })
  body: string;

  @Prop()
  galleryUrls: [string];

  @Prop()
  voiceUrl: string;

  @Prop({ type: VideoModelSchema })
  video: VideoModel;

  @Prop({ type: [String], index: true })
  tags: string[];

  @Prop({ type: LocationModelSchema, index: true })
  location: LocationModel;


}

export const ContentModelSchema = SchemaFactory.createForClass(ContentModel);
0 Answers
Related