Nestjs:猫鼬中子文档数组的正确模式(没有默认_id或重新定义ObjectId)

Ale*_*Dim 3 mongoose mongodb node.js typescript nestjs

我正在使用 Nest.js 并尝试使用包含子文档字段数组的装饰器创建一个架构。

\n

在导入/导出架构并将其转换为模型方面,我没有任何麻烦,直到:

\n

错误

\n

我的文件中收到以下错误service

\n

经过几个小时的谷歌搜索,我发现真正的原因是在array子文档字段背后,而且只有它们。一旦我移除该members字段。架构和模型就可以了。并且不必对以下描述的解决方案或与 相关的任何其他答案extends Mongoose.Document执行任何操作。(如果你已经这样做了

\n

我发现大多数情况与子文档相关,但与数组子文档无关。我想问:

\n

如何使用装饰器通过 mongoose / Typescript 在 Nestjs 中正确创建包含子文档数组的字段?

\n

并删除此错误:

\n
S2344: Type \'Guild\' does not satisfy the constraint \'Document<any, {}>\'. \xc2\xa0\xc2\xa0\n  The types returned by \'delete(...).$where(...).cast(...)\' are incompatible between these types.\n   Type \'UpdateQuery<Guild>\' is not assignable to type \'UpdateQuery<Document<any, {}>>\'.\n     Type \'UpdateQuery<Guild>\' is not assignable to type \'_UpdateQuery<_AllowStringsForIds<LeanDocument<Document<any, {}>>>>\'.\nTypes of property \'$pull\' are incompatible.\n  Type \'PullOperator<_AllowStringsForIds<LeanDocument<Guild>>>\' has no properties in common with type \'PullOperator<_AllowStringsForIds<LeanDocument<Document<any, {}>>>>\'.\n
Run Code Online (Sandbox Code Playgroud)\n

我的架构是:

\n
import { Document, Schema as MongooseSchema } from \'mongoose\';\nimport { Prop, Schema, SchemaFactory } from \'@nestjs/mongoose\';\n\nclass GuildMember {\n  @Prop({ type: String, required: true, lowercase: true })\n  _id: string;\n\n  @Prop({ required: true })\n  id: number;\n\n  @Prop({ required: true })\n  rank: number;\n}\n\n@Schema({ timestamps: true })\nexport class Guild extends Document {\n  @Prop({ type: String, required: true, lowercase: true })\n  _id: string;\n\n  @Prop({ type: MongooseSchema.Types.Array})\n  members: GuildMember[]\n}\n\nexport const GuildsSchema = SchemaFactory.createForClass(Guild);\n
Run Code Online (Sandbox Code Playgroud)\n

我做了什么。

\n

多种方式包括:

\n
    \n
  • Class用装饰器覆盖子文档@Schema()并添加extends Document
  • \n
  • 添加type:到字段@Prop()装饰器:
  • \n
\n
  @Prop({ type: [GuildMember] })\n  members: GuildMember[]\n
Run Code Online (Sandbox Code Playgroud)\n

或相反亦然。它适用ok于原语,但不适用于Class嵌入式文档。

\n
    \n
  • 添加@Prop({ ref: () => GuildMember })
  • \n
\n

并遵循 NestJs 官方文档:

\n
  @Prop({ type: [{ type: MongooseSchema.Types.Array, ref: GuildMember }] })\n  members: GuildMember[]\n
Run Code Online (Sandbox Code Playgroud)\n

它仍然没有帮助。我认为,它可能不仅与 相关mongoose.Document,而且还与另一种类型相关,即:mongoose.DocumentArray

\n

更新:

\n

按照目前的进展。看来问题与field: type默认猫鼬的值有关,而不是@Prop装饰器本身。所以即使我写了这样的东西:

\n
  @Prop()\n  members: Types.Array<GuildMember>\n
Run Code Online (Sandbox Code Playgroud)\n

它仍然给出一个错误。类型导入自:import { Document, Schema as MongooseSchema, Types } from \'mongoose\';

\n

Ale*_*Dim 5

更新:

工作解决方案:

为其他 TS 原语重新定义 _id:

import { Document, Schema as MongooseSchema, Types } from "mongoose";
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

@Schema()
class Pet extends Document  {
  @Prop({ type: Number })
  _id: number;

  @Prop({ type: String })
  name: string;
}

export const PetsSchema = SchemaFactory.createForClass(Pet);
Run Code Online (Sandbox Code Playgroud)

对象字段的父架构数组:

  @Prop({ type: [PetsSchema] })
  pets: Types.Array<Pet>;
Run Code Online (Sandbox Code Playgroud)

ObjectId如果您想完全禁用内置功能

使用以下装饰器并从子模式中@Prop删除:_id

  @Prop({ _id: false, type: [PetsSchema] })
  pets: Types.Array<Pet>;
Run Code Online (Sandbox Code Playgroud)