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

169
Visualizações
Inserting data into a double-nested array

I'll try to keep it short and simple, The schema looks like below...


import mongoose from 'mongoose'

const QuestionSchema = mongoose.Schema({
    questionTitle: { type: String, required: " title"},
    questionBody: { type: String, required: "body"}
    userId: { type: String},
    askedOn: { type: Date, default: Date.now},
    Comment:[{
        commentBody: String,
        userCommented: String,
        userId: String,
        commentedOn: { type: Date, default: Date.now},
    }],
    answer: [{
        answerBody: String,
        userAnswered: String,
        userId: String,
        answeredOn: { type: Date, default: Date.now},
        Comment:[{                      
            commentBody:String,          ////
            userCommented: String,       ////
            userId: String,              ////  
            commentedOn: { type: Date, default: Date.now},         ////
        }]
    }]
})

export default mongoose.model("Question", QuestionSchema)

How do i fill data in the slashed part of code?? ( i.e comment section of answers) i wanted to pass answerId with the comment data, to somehow look for that answerId in whole schema and fill my data into this comment section

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

0

You can do achieve this using $ and $push.

Example:

const updateTheCol =  await Question.updateOne(
    { "answer.userId": "user id" }, 
    { $push: { 
        "answer.$.Comment": { 
            commentBody: "comment body", 
            userCommented: "user commented",
            userId: "user id"
        }
    } 
});
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