Ale*_*Dim 3 mongoose mongodb node.js typescript nestjs
我正在使用 Nest.js 并尝试使用包含子文档字段数组的装饰器创建一个架构。
\n在导入/导出架构并将其转换为模型方面,我没有任何麻烦,直到:
\n\n我的文件中收到以下错误service。
经过几个小时的谷歌搜索,我发现真正的原因是在array子文档字段背后,而且只有它们。一旦我移除该members字段。架构和模型就可以了。并且不必对以下描述的解决方案或与 相关的任何其他答案extends Mongoose.Document执行任何操作。(如果你已经这样做了)
我发现大多数情况与子文档相关,但与数组子文档无关。我想问:
\n并删除此错误:
\nS2344: 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, {}>>>>\'.\nRun Code Online (Sandbox Code Playgroud)\n我的架构是:
\nimport { 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);\nRun Code Online (Sandbox Code Playgroud)\n多种方式包括:
\nClass用装饰器覆盖子文档@Schema()并添加extends Documenttype:到字段@Prop()装饰器: @Prop({ type: [GuildMember] })\n members: GuildMember[]\nRun Code Online (Sandbox Code Playgroud)\n或相反亦然。它适用ok于原语,但不适用于Class嵌入式文档。
@Prop({ ref: () => GuildMember })并遵循 NestJs 官方文档:
\n @Prop({ type: [{ type: MongooseSchema.Types.Array, ref: GuildMember }] })\n members: GuildMember[]\nRun Code Online (Sandbox Code Playgroud)\n它仍然没有帮助。我认为,它可能不仅与 相关mongoose.Document,而且还与另一种类型相关,即:mongoose.DocumentArray
按照目前的进展。看来问题与field: type默认猫鼬的值有关,而不是@Prop装饰器本身。所以即使我写了这样的东西:
@Prop()\n members: Types.Array<GuildMember>\nRun Code Online (Sandbox Code Playgroud)\n它仍然给出一个错误。类型导入自:import { Document, Schema as MongooseSchema, Types } from \'mongoose\';
工作解决方案:
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)