如何使用 NestJS 命名法为猫鼬写下嵌套模式

How to write down nested schemas for mongoose using NestJS nomenclature

我正在学习 NestJS 和 Mongoose。我想知道如何使用 NextJS 命名法在 Mongoose 中编写 down/code 嵌套模式。

传入的数据结构如下所示-

{
    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 post - 之后想出的东西。但我只是在暗中扔石头

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;

好问题,激起了我的好奇心!

我不是Mongoose新手,而是NestJS新手,正在学习使用。我能够正确地做到这一点 运行。

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

export type CatDocument = Cat & Document;


@Schema()
export class Owners {
  @Prop()
  names: [string];
}

@Schema()
export class Cat {
  @Prop()
  name: string;

  @Prop()
  age: number;

  @Prop()
  breed: string;

  @Prop()
  owners: Owners;//schema for owner
}

export const CatSchema = SchemaFactory.createForClass(Cat);

结果!如果我理解正确,它似乎可以解决您的问题。拜托,你需要适应,我不确定你添加的所有这些级别,你将不得不试验!

在您的示例中,您“编译”了子模式,如果我没记错的话,即使在 Mongoose 中使用 express,您也不会这样做!我必须仔细检查一下!对于NestJS,好像不需要!

如果您想要这样的东西:,那就是另一回事了。我对这种方法的关注是使用 populate,我知道如何在 Express 上使用,但不知道如何在 NestJS 上使用。

我发现他们的文档非常丰富,而且他们还有一个存储库。 看这里:https://docs.nestjs.com/techniques/mongodb

更新

user3399180 8 很好地通过电子邮件向我发送了他的最终答复!!

import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { Document } from "mongoose";

@Schema()
class testInfo {
    @Prop()
    title: string;
    @Prop()
    score: number;
    @Prop()
    description: string;
    @Prop()
    time: string;
    @Prop()
    dateOfCreation: string;
}

@Schema()
class OptionContent {
    @Prop()
    answer: string;
    @Prop()
    description: string;
    @Prop()
    id: string;
    @Prop()
    key: string;
    @Prop()
    option: string
}

@Schema()
class Option {
    @Prop()
    option: OptionContent;
}

@Schema()
class Page {
    @Prop()
    question: string;
    @Prop()
    options: Option;
}

@Schema()
class McqStore {
    @Prop()
    page: Page;
}

@Schema()
export class Test {
    @Prop()
    info: testInfo

    @Prop()
    McqStore: McqStore
}

@Schema()
export class TestContainer {
    @Prop({ type: Map })
    name: Test
}

export const TestContainerSchema = SchemaFactory.createForClass(TestContainer);

export type userDocument = Test & Document;

相关: