Nestjs:Mongoose中子文档数组的正确模式(没有default_id或重定义ObjectID) [英] Nestjs: Correct schema for array of subdocuments in mongoose (without default _id or redefine ObjectId)

查看:90
本文介绍了Nestjs:Mongoose中子文档数组的正确模式(没有default_id或重定义ObjectID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

搜索了几个小时后,我发现真正的原因在array子文档字段后面,而且只有它们。只要我删除members字段。架构&;模型很好。,并且与in following描述的解决方案或extends Mongoose.Document相关的any other answers无关。(如果您已经这样做了)

我发现的大多数案例都与子文档相关,但与数组子文档无关。我想问一下:

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

并取消设定此错误的种子:

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()装饰符:
  @Prop({ type: [GuildMember] })
  members: GuildMember[]

或反之亦然。它适用于ok基元,但不适用于Class嵌入文档。

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

并遵循NestJs官方文档:

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

还是没有用。我想它可能是相关的,不只是mongoose.Document,还有一种类型,就是mongoose.DocumentArray

更新:

根据目前的进展情况。问题似乎与default mongoose的field: type值有关,而与@Prop修饰符本身无关。所以,即使我写了这样的东西:

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

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

推荐答案

更新:

工作解决方案:

对其他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);

对象的父架构数组字段:

  @Prop({ type: [PetsSchema] })
  pets: Types.Array<Pet>;

如果要完全禁用内置ObjectId

使用以下@Prop修饰符,从子架构中删除_id

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

这篇关于Nestjs:Mongoose中子文档数组的正确模式(没有default_id或重定义ObjectID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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