我想自动添加_id到数组中的所有对象。我正在使用nestjs框架的mongoose。
我的schema看起来像
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
export type PropModel = newSchemaDetail & Document;
@Schema()
export class Prop3 {
@Prop({ type: String, required: true })
prop3.1: string;
@Prop({ type: String, required: true })
prop3.2: string;
}
@Schema()
export class Prop4{
@Prop({ type: String, required: true })
prop4.1: string;
@Prop({ type: String, required: true })
prop4.2: string;}
@Schema()
export class newSchemaDetail {
@Prop({ type: String, required: true })
prop1: string;
@Prop({ type: String, required: false })
prop2: string;
@Prop({ type: Array<Prop3>, required: true })
prop3: Prop3[];
@Prop({ type: Prop3, required: true })
prop4:Prop4
}
export const PropSchema = SchemaFactory.createForClass(newSchemaDetail);
结果
{
"prop1": "xyz",
"prop2": "abc",
"prop3": [
{
"prop3.1": "kgkhkmfe",
"prop3.2": "nbjfmlkgh"
}
],
"prop4":{"_id":"646b99d74abf7c25e21tx85","prop4.1":"test","prop4.2":"test2"},
"_id": "646c99e74bcf7b25d21cyb85",
"__v": 0
}
预期
{
"prop1": "xyz",
"prop2": "abc",
"prop3": [
{
"_id":"646b99d47abf7c56e21tx89"
"prop3.1": "kgkhkmfe",
"prop3.2": "nbjfmlkgh"
}
],
"prop4":{"_id":"646b99d74abf7c25e21tx85","prop4.1":"test","prop4.2":"test2"},
"_id": "646c99e74bcf7b25d21cyb85",
"__v": 0
}
为什么@Schema()
不能处理对象数组,或者我做错了什么。有人能告诉我如何得到我期望的结果吗?
先谢谢你了!
2条答案
按热度按时间ycl3bljg1#
首先,你应该像这样为Prop3创建一个模式:
并将
@Prop({ type: Array<Prop3>, required: true })
行替换为以下内容。ua4mk5z42#
尝试从
AbstractDocument
扩展