0

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

1 Answer 1

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"
        }
    } 
});
Sign up to request clarification or add additional context in comments.

2 Comments

I would floss a tiger’s teeth, that’s how much I appreciate you...thank you so much
stackoverflow.com/questions/70908967/… ...May you please help me out with this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.