Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Crear tabla sin crear una nueva colección en MongoDB, NestJs

Lucho con el problema. En mi aplicación, tengo que crear un pedido con varios atributos y uno de ellos es una matriz de productos pedidos. En la matriz orderProduct, cada objeto debe tener productId y la cantidad.

Pero no quiero crear una nueva colección llamada OrderedProduct. En la solución actual, está creando ambos: producto pedido vacío y pedidos. y tengo un problema

 "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\""

esquemas

 @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 }

No he sido capaz de hacer esto. ¡Seré apreciado por su ayuda!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que necesitas usar el esquema de tu subobjeto. algo como:

 @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] }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!