nest.js-在Mongoose模式中创建索引 [英] Nest.js - Create index in mongoose schema
本文介绍了nest.js-在Mongoose模式中创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Nest.js在Mongoose架构中创建属性索引?
我尝试将索引作为属性选项添加,但尚未创建索引:
@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);
我也尝试过这种方式:
export const MySchema = SchemaFactory.createForClass(Schema).index({ type: 1 });
两者均未按预期工作。
执行此操作的方法是什么?
谢谢
推荐答案
使用以下选项创建索引
@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);
定义架构时使用useCreateIndex
标志
或在创建连接对象时全局设置相同的标志
{
uri: `....`,
user: ,
pass: ,
//useNewUrlParser: true,
useCreateIndex: true,
//useUnifiedTopology: true,
//useFindAndModify: false,
retryAttempts: 3
}
还添加了可能需要的其他注释标志。
这篇关于nest.js-在Mongoose模式中创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文