node.js - Nest.js - 在猫鼬模式中创建索引
问题描述
如何使用 Nest.js 在猫鼬模式中创建属性索引?
我尝试将索引添加为属性选项,但尚未创建索引:
@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
}
还添加了其他可能需要的注释标志。
推荐阅读
- excel - 将字符串变量设置为单元格——应用程序定义的或对象定义的错误
- sql - 如何修复“无法删除对象 'Teams',因为它被 FOREIGN KEY 约束引用”
- graphql - 如何在一个类型中添加多个解析器(Apollo-server)
- java - Collections.sort 不改变列表
- java - MouseClicked() 方法不适用于寻路算法?
- sql - 如何避免使用 for next 方法获取行数
- angular - Angular SSR - 要导入的文件未找到或不可读:
- php - 查询所有年份,但只查询一次
- javascript - 在电话系统中集成 Zendesk Talk API 代码
- android - 如何处理 TextInputEditText 的复合可绘制对象上的 onclick 事件?