NestJS MongoDB 嵌套对象模式
NestJS MongoDB nested object schema
我目前运行代码:
export class SystemInformationContent {
createdAt: number;
createdBy: User | mongoose.Schema.Types.ObjectId | null;
updatedAt?: number;
updatedBy?: User | mongoose.Schema.Types.ObjectId | null;
}
@Schema()
export class SystemInformation {
@Prop(
raw({
createdAt: { type: Number, required: true },
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
updatedAt: { type: Number, default: 0 },
updatedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
default: null,
},
}),
)
system: SystemInformationContent;
}
我没有找到任何方法来“扩展”SystemInformationContent
的架构,因此在 @Prop()
装饰器中使用了 raw()
函数,但我想知道是否有做这样的事情的方法:
export class SystemInformationContent {
@Prop({ required: true })
createdAt: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
createdBy: User | mongoose.Schema.Types.ObjectId | null;
@Prop({ default: 0 })
updatedAt?: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null })
updatedBy?: User | mongoose.Schema.Types.ObjectId | null;
}
@Schema()
export class SystemInformation {
@Prop(???)
system: SystemInformationContent;
}
我没有发现任何考虑到 SystemInformationContent
.
架构的 SystemInformation.system
@Prop()
的工作
你们知道除了 raw
还有其他方法吗?或者我是否遗漏了什么?
编辑:我的 NestJS 应用程序的所有 类 都在扩展 SystemInformation,因此它们看起来都像:
{
...,
system: {
createdAt: 1616778310610,
createdBy: "605e14469d860eb1f0641cad",
editedAt: 0,
createdBy: null,
},
}
如果您正在使用 typegoose
(7.0 或更高版本),那么如果您在 tsconfig
中启用了 emitDecoratorMetadata
,那么您当前拥有的应该足够了
typegoose 示例(下面的代码使用 ~7.4 的选项扩展):
export class SystemInformationContent {
@Prop({ required: true })
createdAt: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: () => User })
createdBy: Ref<User>;
@Prop({ default: 0 })
updatedAt?: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: () => User }) // default is "undefined"
updatedBy?: Ref<User>;
}
export class SystemInformation {
@Prop() // thanks to "emitDecoratorMetadata" no explicit types are needed
system: SystemInformationContent;
// but if wanting to do explicit types
@Prop({ type: () => SystemInformationContent })
system: SystemInformationContent;
}
PS: 我不知道你的函数 raw
来自哪里,但这不是 typegoose 函数
如果我做对了,这就是你想要的。您只是错过了 SystemInformationContent
的 @Schema()
注释,并且需要在任何您想要的地方继承这个模式。我有一个类似的问题。所以,如果这不是您要求的,请告诉我。
@Schema()
export class SystemInformationContent {
// put your @Prop() attributes here, that need to be available in every other schema
}
@Schema()
export class SystemInformation extends SystemInformationContent {
// no need to do anything else here
// the attributes are inherited from SystemInformationContent
}
找到解决方案!
我将 SystemInformationContent
编辑为:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import * as mongoose from 'mongoose';
import { User } from '~/schemas/user.schema';
@Schema({ _id: false })
export class SystemInformationContent {
@Prop({ type: Number, required: true })
createdAt: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
createdBy: User;
@Prop({ type: Number, default: 0 })
updatedAt?: number;
@Prop({
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
default: null,
})
updatedBy?: User;
}
export const SystemInformationContentSchema = SchemaFactory.createForClass(
SystemInformationContent,
);
然后在 SystemInformation 中我编辑为:
import { Prop, Schema } from '@nestjs/mongoose';
import {
SystemInformationContent,
SystemInformationContentSchema,
} from '~/schemas/systemInformationContent.schema';
@Schema()
export default class SystemInformation {
@Prop({ required: true, type: SystemInformationContentSchema })
system: SystemInformationContent;
}
现在一切正常,我使用 @Schema({ _id: false })
删除了 SchemaFactory 生成的 ID,所以我最终在数据库中得到了它:
{
...,
"system": {
"updatedBy": null,
"updatedAt": 0,
"createdAt": 1616847116986,
"createdBy": {
"$oid": "605f210cc9fe3bcbdf01c95d"
}
},
...,
}
我目前运行代码:
export class SystemInformationContent {
createdAt: number;
createdBy: User | mongoose.Schema.Types.ObjectId | null;
updatedAt?: number;
updatedBy?: User | mongoose.Schema.Types.ObjectId | null;
}
@Schema()
export class SystemInformation {
@Prop(
raw({
createdAt: { type: Number, required: true },
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
updatedAt: { type: Number, default: 0 },
updatedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
default: null,
},
}),
)
system: SystemInformationContent;
}
我没有找到任何方法来“扩展”SystemInformationContent
的架构,因此在 @Prop()
装饰器中使用了 raw()
函数,但我想知道是否有做这样的事情的方法:
export class SystemInformationContent {
@Prop({ required: true })
createdAt: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
createdBy: User | mongoose.Schema.Types.ObjectId | null;
@Prop({ default: 0 })
updatedAt?: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null })
updatedBy?: User | mongoose.Schema.Types.ObjectId | null;
}
@Schema()
export class SystemInformation {
@Prop(???)
system: SystemInformationContent;
}
我没有发现任何考虑到 SystemInformationContent
.
SystemInformation.system
@Prop()
的工作
你们知道除了 raw
还有其他方法吗?或者我是否遗漏了什么?
编辑:我的 NestJS 应用程序的所有 类 都在扩展 SystemInformation,因此它们看起来都像:
{
...,
system: {
createdAt: 1616778310610,
createdBy: "605e14469d860eb1f0641cad",
editedAt: 0,
createdBy: null,
},
}
如果您正在使用 typegoose
(7.0 或更高版本),那么如果您在 tsconfig
emitDecoratorMetadata
,那么您当前拥有的应该足够了
typegoose 示例(下面的代码使用 ~7.4 的选项扩展):
export class SystemInformationContent {
@Prop({ required: true })
createdAt: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: () => User })
createdBy: Ref<User>;
@Prop({ default: 0 })
updatedAt?: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: () => User }) // default is "undefined"
updatedBy?: Ref<User>;
}
export class SystemInformation {
@Prop() // thanks to "emitDecoratorMetadata" no explicit types are needed
system: SystemInformationContent;
// but if wanting to do explicit types
@Prop({ type: () => SystemInformationContent })
system: SystemInformationContent;
}
PS: 我不知道你的函数 raw
来自哪里,但这不是 typegoose 函数
如果我做对了,这就是你想要的。您只是错过了 SystemInformationContent
的 @Schema()
注释,并且需要在任何您想要的地方继承这个模式。我有一个类似的问题。所以,如果这不是您要求的,请告诉我。
@Schema()
export class SystemInformationContent {
// put your @Prop() attributes here, that need to be available in every other schema
}
@Schema()
export class SystemInformation extends SystemInformationContent {
// no need to do anything else here
// the attributes are inherited from SystemInformationContent
}
找到解决方案!
我将 SystemInformationContent
编辑为:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import * as mongoose from 'mongoose';
import { User } from '~/schemas/user.schema';
@Schema({ _id: false })
export class SystemInformationContent {
@Prop({ type: Number, required: true })
createdAt: number;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
createdBy: User;
@Prop({ type: Number, default: 0 })
updatedAt?: number;
@Prop({
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
default: null,
})
updatedBy?: User;
}
export const SystemInformationContentSchema = SchemaFactory.createForClass(
SystemInformationContent,
);
然后在 SystemInformation 中我编辑为:
import { Prop, Schema } from '@nestjs/mongoose';
import {
SystemInformationContent,
SystemInformationContentSchema,
} from '~/schemas/systemInformationContent.schema';
@Schema()
export default class SystemInformation {
@Prop({ required: true, type: SystemInformationContentSchema })
system: SystemInformationContent;
}
现在一切正常,我使用 @Schema({ _id: false })
删除了 SchemaFactory 生成的 ID,所以我最终在数据库中得到了它:
{
...,
"system": {
"updatedBy": null,
"updatedAt": 0,
"createdAt": 1616847116986,
"createdBy": {
"$oid": "605f210cc9fe3bcbdf01c95d"
}
},
...,
}