How do I create an index of property in mongoose schema using Nest.js?
I tried to add index as a property option, But the index hasn't been created:
@Schema()
export class Schema extends Document {
@Prop()
_id: string;
@Prop({required: true, index: true})
type: string;
@Prop()
creationDate: string;
@Prop()
name: string;
}
export const MySchema = SchemaFactory.createForClass(Schema);
I tried this way too:
export const MySchema = SchemaFactory.createForClass(Schema).index({ type: 1 });
Both doesn't work as expected.
What is the way to do that?
Thanks
Use following option to create index
@Schema({useCreateIndex: true})
export class Schema extends Document {
@Prop()
_id: string;
@Prop({required: true, index: true})
type: string;
@Prop()
creationDate: string;
@Prop()
name: string;
}
export const MySchema = SchemaFactory.createForClass(Schema);
use useCreateIndex
flag either when defining the schema
or globally set the same flag when creating the connection object
{
uri: `....`,
user: ,
pass: ,
//useNewUrlParser: true,
useCreateIndex: true,
//useUnifiedTopology: true,
//useFindAndModify: false,
retryAttempts: 3
}
Added other commented flags as well which could be required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With