0

I'm trying to add elements into an array of objects, but I', having problems with adding more than 1 object.

This is the Model of my groups collection

const userSchema = mongoose.Schema({
name: {type: String, required: true},
password: {type: String, required: true},
description: {type: String, required: true},
scope: String,
groupTeacher: {
    type: mongoose.Types.ObjectId,
    ref: 'users',
},
quizzes:[
    {
        _id: mongoose.Types.ObjectId,
        name: String,
        tier: Number,
        category: String,
        questions:[
            {
                _id: mongoose.Types.ObjectId,
                title: String,
                question: String,
                correctAnswer: String,
                answer1: String,
                answer2: String,
                answer3: String,
                answer4: String
            }
        ],
        usersAttempted:[
            {
                _id: {
                    type: mongoose.Types.ObjectId,
                    ref: 'users',
                },
                //userEmail: String,
                correctAnswers: Number,
                wrongAnswers: Number,
                answers: [{
                    questionTitle: String,
                    correctAnswer: String,
                    usersAnswer: String
                }]

I want to add elements into the 'usersAttempted' array of objects. I have problems with the 'answers' part. The problem is that there are many questions answers but, I only got it working for adding 1 questions answers.

This is how I did it.

    const result = await groupsModel.updateOne({
        "_id": groupId,
        "quizzes._id": quizId,
        "quizzes.usersAttempted._id": {$ne: userId}
    }, 
    {
        $addToSet:{
            "quizzes.$.usersAttempted":{
                _id:userId,
                correctAnswers: questionsCorrect,
                wrongAnswers:   questionsWrong,
                answers:{
                    questionTitle:  answers[0].questionTitle,
                    correctAnswer:  answers[0].correctAnswer,
                    usersAnswer:    answers[0].usersAnswer
                }
            }}});

Answers is an array of objects

Answers array of objects

Thank you for all your help.

EDIT: #1
Small explanation: I have an array of objects, that I want to add to "usersAttempted" but I have problems with adding more than 1 answer.

EDIT: #2

I managed to add array of objects into the collection, but it is done with indexes.

        const result = await groupsModel.updateOne({
        "_id": groupId,
        "quizzes._id": quizId,
        "quizzes.usersAttempted._id": {$ne: userId}
    }, 
    {
        $addToSet:{
            "quizzes.$.usersAttempted":{
                _id:userId,
                correctAnswers: questionsCorrect,
                wrongAnswers:   questionsWrong,
                answers:[{
                    questionTitle:  answers[0].questionTitle,
                    correctAnswer:  answers[0].correctAnswer,
                    usersAnswer:    answers[0].usersAnswer
                },
                {
                    questionTitle:  answers[1].questionTitle,
                    correctAnswer:  answers[1].correctAnswer,
                    usersAnswer:    answers[1].usersAnswer
                },
                {
                    questionTitle:  answers[2].questionTitle,
                    correctAnswer:  answers[2].correctAnswer,
                    usersAnswer:    answers[2].usersAnswer
                }]
                    
            }}});

Now I want to find a way of adding an array with x number of objects.

4
  • Why are you only adding a single answer instead of the entire array? Commented Mar 3, 2021 at 0:36
  • because I'm not sure how to add the whole array :D Thats why the question Commented Mar 3, 2021 at 12:08
  • It would be helpful to post the actual code instead of images of your code. Use the {} button on the toolbar of the editor to designate it as a code block Commented Mar 3, 2021 at 12:51
  • I tried to find online how to add an entire array. But I couldn't find anything. Commented Mar 3, 2021 at 14:24

1 Answer 1

1

What happens when you try:

        $addToSet:{
        "quizzes.$.usersAttempted":{
            _id:userId,
            correctAnswers: questionsCorrect,
            wrongAnswers:   questionsWrong,
            answers                    
        }}});

It seems to me that you would be adding the entire answer array...

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, that works. I tried that before but the problem was that the names of fields compared to names in the object were different. Before it was "answers.title" (object) and "questionTitle" (collection). I changed the names so they are the same and it worked perfectly. Thank you. I guess this was your easiest solve :D
What if I want to update or change the existing value of any field, then how can I do that?

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.