【问题标题】:How to write down nested schemas for mongoose using NestJS nomenclature [closed]如何使用 NestJS 命名法为猫鼬写下嵌套模式 [关闭]
【发布时间】: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;

【问题讨论】:

标签: javascript typescript mongodb schema nestjs


【解决方案1】:

很好的问题,它激发了我的好奇心!

我不是 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,似乎不需要!

如果您想要类似:Mongoose Subdocuments in Nest.js,那就是另一回事了。我对这种方法的关注是使用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;

相关:

  1. NestJS - How to create nested schema with decorators

【讨论】:

  • 完美答案!将此标记为正确答案。
  • 很高兴能提供帮助,如果我们能提供帮助,那就太贵了! ??
  • 嗨! @Jorge Guerra Pires。尽管您的回答对我有很大帮助,但我还是根据自己的需要进行了一些更改以适应答案。我正在等待此主题再次打开,以便我可以将我的代码发布在未来读者的答案中。
  • 很高兴我提供了帮助,我对其进行了编辑,以便它可以重新打开。不知道他们是否会重新开放。我正在考虑在我的频道上创建一个视频,喜欢你的问题!请将最终答案发送至 jorgeguerrapires@yahoo.com.br
猜你喜欢
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 2015-09-21
  • 2021-06-07
  • 1970-01-01
  • 2018-06-23
  • 2019-08-07
相关资源
最近更新 更多