I am learning NestJS, and Mongoose. I was wondering how to write down/code nested schemas in Mongoose using NextJS nomenclature.
Incoming data structure looks like this -
{
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
}
}
}
}
}
}
Challenge: writing down that using NestJS internal APIs.
I am using NestJS and Mongoose. I want to write a schema for the data structure given above. I can't find examples for nested schemas. Any insigth is welcome.
I am a beginner in all NestJS, Mongoose and MongoDB. So please don't assume that I know something. Thus, any insight on Mongoose as well is welcome.
Thanks a lot.
Edit - Here's something I came up with after following this SO post - Mongoose Subdocuments in Nest.js . But I am just throwing stones in the dark.
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;