Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
Create table without creating new collection in MongoDB, NestJs

I struggle with the problem. In my app, I have to create an order with several attributes and one of them is an array of ordered products. In the orderedProduct array, each object should have productId and the amount.

But, I don't want to create a new collection called OrderedProduct. In the current solution, it is creating both: empty orderedproduct and orders. And I have an issue

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

Schemas

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

I have not been able to do this. I will be appreciated for your help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you need to use the schema of your sub object. something like:

@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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda