【发布时间】:2021-04-01 21:56:30
【问题描述】:
如何使用 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 });
两者都没有按预期工作。
这样做的方法是什么?
谢谢
【问题讨论】:
-
两种解决方案都应该有效,
Document是从猫鼬导入的吗? -
是的,文档是从 'mongoose' 导入的。运行该服务时,我收到此警告:DeprecationWarning: collection.ensureIndex is deprecated。改用 createIndexes,所以我猜,我的索引出了点问题,但这个集合中唯一的索引是“_id”
-
如果已经在 mongodb 中设置了字段,您是否需要告诉架构一个字段是索引?