创建 table 而不在 MongoDB 中创建新集合,NestJs
Create table without creating new collection in MongoDB, NestJs
我正在努力解决这个问题。在我的应用程序中,我必须创建一个具有多个属性的订单,其中一个是一系列订购产品。在 orderedProduct 数组中,每个对象都应该有 productId 和数量。
但是,我不想创建一个名为 OrderedProduct 的新集合。在当前的解决方案中,它同时创建了:空的订购产品和订单。我有一个问题
"Cast to ObjectId failed for value \"{\n productId: { _id: '0a8b51d0-5bb1-4ab6-83d0-5b0c15f44e2b' },\n amount: 3\n}\" (type Object) at path \"_id\" for model \"OrderedProduct\""
架构
@Schema()
export class Order {
@Prop()
_id: string
@Prop({ type: Date, default: Date.now })
confirmedDate: Date
@Prop({ type: Types.ObjectId, ref: OrderStatus.name })
orderStatus: OrderStatus
@Prop()
userName: string
@Prop()
email: string
@Prop()
phoneNumber: string
@Prop({ type: Types.ObjectId, ref: OrderedProduct.name })
orderedProductArray: [OrderedProduct]
}
@Schema()
export class OrderedProduct {
@Prop(() => Int)
amount: string
@Prop({ type: Types.ObjectId, ref: Cloth.name })
product: Cloth
}
我没能做到这一点。非常感谢您的帮助!
我认为您需要使用子对象的架构。类似于:
@Schema()
export class OrderedProduct {
@Prop(() => Int)
amount: string
@Prop({ type: Types.ObjectId, ref: Cloth.name })
product: Cloth
}
export const OrderedProductSchema = SchemaFactory.createForClass(OrderedProduct);
@Schema()
export class Order {
@Prop()
_id: string
@Prop({ type: Date, default: Date.now })
confirmedDate: Date
@Prop({ type: Types.ObjectId, ref: OrderStatus.name })
orderStatus: OrderStatus
@Prop()
userName: string
@Prop()
email: string
@Prop()
phoneNumber: string
@Prop({ type: [OrderedProductSchema] })
orderedProductArray: [OrderedProduct]
}
我正在努力解决这个问题。在我的应用程序中,我必须创建一个具有多个属性的订单,其中一个是一系列订购产品。在 orderedProduct 数组中,每个对象都应该有 productId 和数量。
但是,我不想创建一个名为 OrderedProduct 的新集合。在当前的解决方案中,它同时创建了:空的订购产品和订单。我有一个问题
"Cast to ObjectId failed for value \"{\n productId: { _id: '0a8b51d0-5bb1-4ab6-83d0-5b0c15f44e2b' },\n amount: 3\n}\" (type Object) at path \"_id\" for model \"OrderedProduct\""
架构
@Schema()
export class Order {
@Prop()
_id: string
@Prop({ type: Date, default: Date.now })
confirmedDate: Date
@Prop({ type: Types.ObjectId, ref: OrderStatus.name })
orderStatus: OrderStatus
@Prop()
userName: string
@Prop()
email: string
@Prop()
phoneNumber: string
@Prop({ type: Types.ObjectId, ref: OrderedProduct.name })
orderedProductArray: [OrderedProduct]
}
@Schema()
export class OrderedProduct {
@Prop(() => Int)
amount: string
@Prop({ type: Types.ObjectId, ref: Cloth.name })
product: Cloth
}
我没能做到这一点。非常感谢您的帮助!
我认为您需要使用子对象的架构。类似于:
@Schema()
export class OrderedProduct {
@Prop(() => Int)
amount: string
@Prop({ type: Types.ObjectId, ref: Cloth.name })
product: Cloth
}
export const OrderedProductSchema = SchemaFactory.createForClass(OrderedProduct);
@Schema()
export class Order {
@Prop()
_id: string
@Prop({ type: Date, default: Date.now })
confirmedDate: Date
@Prop({ type: Types.ObjectId, ref: OrderStatus.name })
orderStatus: OrderStatus
@Prop()
userName: string
@Prop()
email: string
@Prop()
phoneNumber: string
@Prop({ type: [OrderedProductSchema] })
orderedProductArray: [OrderedProduct]
}