Nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID) [英] Nestjs: mongoose schema (decorators) with array subdocument field (without default _id as ObjectID)

查看:26
本文介绍了Nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with Nest.js and trying to create a Schema with decorators which contain array of sub-document field.

导入/导出架构并将其转换为模型,我没有任何问题,直到:

I don't have any troubles, with importing/export the Schema and converting it to a model, until:

我在 service 文件中收到以下错误.

I receive the following error in my service file.

经过数小时的谷歌搜索,我发现真正的原因在于 array 子文档字段背后,而且只有它们.一旦我删除了 members 字段.模式 &模型会很好.并且不必对描述的解决方案做任何事情在以下,或任何其他答案extends Mongoose.Document 相关.(如果你已经做过了)

After hours of googling, I discover that the real reason is behind the array sub-document fields, and only with them. As soon, as I remove the members field. the schema & model will be fine. And have to do nothing with the solution described in following, or any other answers relevant with extends Mongoose.Document. (If you have already done it)

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

Most of cases, that I found, are relevant with sub-documents, but not array sub-document. And I'd like to ask:

并清除此错误:

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

我的架构是:

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

class GuildMember {
  @Prop({ type: String, required: true, lowercase: true })
  _id: string;

  @Prop({ required: true })
  id: number;

  @Prop({ required: true })
  rank: number;
}

@Schema({ timestamps: true })
export class Guild extends Document {
  @Prop({ type: String, required: true, lowercase: true })
  _id: string;

  @Prop({ type: MongooseSchema.Types.Array})
  members: GuildMember[]
}

export const GuildsSchema = SchemaFactory.createForClass(Guild);

我做了什么.

各种方式包括:

  • @Schema()装饰器覆盖子文档Class并添加extends Document
  • type: 添加到字段 @Prop() 装饰器:
  • Cover sub-document Class with @Schema() decorator and adding extends Document
  • Adding type: to field @Prop() decorator:
  @Prop({ type: [GuildMember] })
  members: GuildMember[]

反之亦然.ok 适用于原语,但不适用于 Class 嵌入文档.

or vice-versa. It's ok for primitives, but not for Class embedded documents.

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

并遵循 NestJs 官方文档:

And following the official NestJs docs:

  @Prop({ type: [{ type: MongooseSchema.Types.Array, ref: GuildMember }] })
  members: GuildMember[]

它仍然没有帮助.我想,它可能是相关的,不仅与 mongoose.Document 相关,还与另一种类型相关,即:mongoose.DocumentArray

It still doesn't help. I thought, that it could be relevant, not just with mongoose.Document, but also another type, which is: mongoose.DocumentArray

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

According to current progress. It seems that problem is relevant with the field: type value of default mongoose, not the @Prop decorator itself. So even if I write something like that:

  @Prop()
  members: Types.Array<GuildMember>

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

it still gives an error. Type is imported from: import { Document, Schema as MongooseSchema, Types } from 'mongoose';

推荐答案

你的 members 属性不是一个简单的数组.它是子文档的集合,应该声明为 [SchemaTypes.ObjectId] 它将通过默认的 mongo ObjectID 实现带有 _id 字段的子文档价值:

Your members prop is not a simple array. It is a collection of sub docs and should be declared as [SchemaTypes.ObjectId] which will implement sub documents with _id field via default mongo ObjectID value:

@Prop({ type: [SchemaTypes.ObjectId], ref: 'GuildMember'})
members: GuildMember[]

这篇关于Nestjs:带有数组子文档字段的猫鼬模式(装饰器)(没有默认的 _id 作为 ObjectID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆