【发布时间】:2021-08-23 04:37:21
【问题描述】:
我正在学习 NestJS 和 Mongoose。我想知道如何使用 NextJS 命名法在 Mongoose 中写下/编码嵌套模式。
传入的数据结构是这样的-
{
something: {
info: {
title: string,
score: number,
description: string,
time: string,
DateOfCreation: string
},
Store: {
item: {
question: string,
options: {
item: {
answer: string,
description: string,
id: string,
key: string,
option: string
}
}
}
}
}
}
挑战:使用 NestJS 内部 API 写下来。
我正在使用 NestJS 和 Mongoose。我想为上面给出的数据结构编写一个模式。 我找不到嵌套架构的示例。欢迎任何见解。
我是所有 NestJS、Mongoose 和 MongoDB 的初学者。所以请不要以为我知道些什么。因此,也欢迎任何关于 Mongoose 的见解。
非常感谢。
编辑 - 这是我在关注此 SO 帖子后想出的东西 - Mongoose Subdocuments in Nest.js。但我只是在黑暗中扔石头。
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
@Schema()
export class Cat {
@Prop()
name: string
}
export const CatSchema = SchemaFactory.createForClass(Cat);
@Schema()
class testInfo {
@Prop()
title: string;
@Prop()
score: number;
@Prop()
description: string;
@Prop()
time: string;
@Prop()
DateOfCreation: string;
}
const testInfoSchema = SchemaFactory.createForClass(testInfo);
@Schema()
class OptionContent {
@Prop()
answer: string;
@Prop()
description: string;
@Prop()
id: string;
@Prop()
key: string;
@Prop()
option: string
}
const OptionContentSchema = SchemaFactory.createForClass(OptionContent);
@Schema({ strict: false })
class Option {
@Prop({ type: OptionContentSchema })
item: OptionContent;
}
const OptionSchema = SchemaFactory.createForClass(Option);
@Schema({ strict: false })
class Page {
@Prop()
question: string;
@Prop({ type: OptionSchema })
options: Option;
}
const PageSchema = SchemaFactory.createForClass(Page);
@Schema({ strict: false })
class McqStore {
@Prop({ type: PageSchema })
item: Page;
}
const McqStoreSchema = SchemaFactory.createForClass(McqStore);
@Schema()
export class Test {
@Prop({ type: testInfoSchema })
info: testInfo
@Prop({ type: McqStoreSchema })
McqStore: McqStore
}
const TestSchema = SchemaFactory.createForClass(Test);
@Schema()
export class TestContainer {
@Prop({ type: TestSchema })
name: Test
}
export const TestContainerSchema = SchemaFactory.createForClass(TestContainer);
export type userDocument = TestContainer & Document;
【问题讨论】:
-
很抱歉没有。虽然可以在 NestJS 中使用 mongoose 编写东西,但我想以 NestJS 的方式编写模式。我找不到该特定问题的任何示例。
标签: javascript typescript mongodb schema nestjs