title: mongodb add objects into array of object
body: <p>I'm trying to add elements into an array of objects, but I', having problems with adding more than 1 object.</p>
  <p>This is the Model of my groups collection</p>
  <pre><code>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
                  }]
  </code></pre>
  <p>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.</p>
  <p>This is how I did it.</p>
  <pre><code>    const result = await groupsModel.updateOne({
          &quot;_id&quot;: groupId,
          &quot;quizzes._id&quot;: quizId,
          &quot;quizzes.usersAttempted._id&quot;: {$ne: userId}
      }, 
      {
          $addToSet:{
              &quot;quizzes.$.usersAttempted&quot;:{
                  _id:userId,
                  correctAnswers: questionsCorrect,
                  wrongAnswers:   questionsWrong,
                  answers:{
                      questionTitle:  answers[0].questionTitle,
                      correctAnswer:  answers[0].correctAnswer,
                      usersAnswer:    answers[0].usersAnswer
                  }
              }}});
  </code></pre>
  <p>Answers is an array of objects</p>
  <p><a href="https://i.stack.imgur.com/iZxMD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iZxMD.png" alt="Answers array of objects" /></a></p>
  <p>Thank you for all your help.</p>
  <p>EDIT: #1<br>
  Small explanation:
  I have an array of objects, that I want to add to &quot;usersAttempted&quot; but I have problems with adding more than 1 answer.</p>
  <p>EDIT: #2 <br></p>
  <p>I managed to add array of objects into the collection, but it is done with indexes.</p>
  <pre><code>        const result = await groupsModel.updateOne({
          &quot;_id&quot;: groupId,
          &quot;quizzes._id&quot;: quizId,
          &quot;quizzes.usersAttempted._id&quot;: {$ne: userId}
      }, 
      {
          $addToSet:{
              &quot;quizzes.$.usersAttempted&quot;:{
                  _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
                  }]
                      
              }}});
  </code></pre>
  <p>Now I want to find a way of adding an array with x number of objects.</p>