【问题标题】:How to add unique value to mongoose subdocument with nestjs?如何使用nestjs为猫鼬子文档添加唯一值?
【发布时间】:2022-11-10 04:46:05
【问题描述】:

我想将我的 express api 中的猫鼬模式转换为嵌套 js。

const levelSchema = new Schema(
  {
    title: {
      type: String,
      required: true,
    },
  },
  {
    timestamps: true,
  }
);
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";

export type LevelDocument = Level & Document;

@Schema({
  timestamps: true,
})
export class Level {
  @Prop({
    type: [
      {
        locale: { type: String, enum: ["fr", "en"] },
        title: { type: String, unique: true },
      },
    ],
  })
  translations: object[];

  @Prop({ default: false })
  visibility: Boolean;
}

export const LevelSchema = SchemaFactory.createForClass(Level);

它按预期工作,但由于某种原因我无法为标题添加唯一键?我没有任何错误,它似乎只是忽略了unique: true 规则。

【问题讨论】:

    标签: mongoose nestjs


    【解决方案1】:

    首先,您的 express 架构和 NestJS 架构之间存在差异。请更具体。 第二件事是,您的架构对我来说非常适合。

    当我尝试复制标题字段时它会抛出错误。

    尝试手动重新索引集合或删除集合并重新启动服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-12
      • 2019-12-20
      • 1970-01-01
      • 2021-05-19
      • 2021-07-28
      • 2021-05-10
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多