title: MongoDB/Mongoose: AddToSet an array into an array field, but also addToSet a nested array field value if the parent object already exists
body: <p>I am building an education application and I am trying to add/update a field which is an array of objects with addToSet from a javascript array, and if the object already exists (matched with objectId) I want to update the already existing object's array (addToSet) and change another field of that same object.</p>
  <p>My model looks like this (simplified):</p>
  <pre><code>const course = new Schema(
  {
      events: [
          {
              type: Schema.Types.ObjectId,
              ref: 'event'
          }
      ],
      students: [
          {
              user: {
                  type: Schema.Types.ObjectId,
                  ref: 'user'
              },
              status: {
                  type: String,
                  enum: ['notBooked', 'booked', 'attended', 'completed']
              },
              events: [
                  {
                      type: Schema.Types.ObjectId,
                      ref: 'event'
                  }
              ]
          }
      ],
  });
  </code></pre>
  <p>And ideally I would like to use an updateOne query to both addToSet to the course's list of events, while also updating the students list.</p>
  <p>Currently I am using this code to accomplish my updates by first finding the course and then using javascript to update it, which works:</p>
  <pre><code>const studentsToAdd = this.attendees.map((attendee) =&gt; ({
      user: attendee._id,
      status: 'booked',
      events: [this._id]
  }));
  const studentIds = studentsToAdd.map((student) =&gt; student.user);
  
  const course = await courseModel.findById(this.course);
  console.log(studentIds);
  
  course.events.push(this._id);
  course.students.forEach((student) =&gt; {
      if (studentIds.some((s) =&gt; s.equals(student.user))) {
          student.events.push(this._id);
          student.status = 'booked';
          studentsToAdd.splice(studentsToAdd.indexOf(studentsToAdd.find((s) =&gt; s.user.equals(student.user))), 1);
      }
  });
  
  course.students.push(...studentsToAdd);
  
  course.save();
  </code></pre>
  <p>However I am curious if it is possible to solve this using a single updateOne on the courseModel schema.</p>
  <p>This is part of a mongoose middleware function, and &quot;this&quot; references an event object, which has its own _id, attendees of the event, and the course ID (named course).</p>
  <p>The goal is to add the student object part of studentsToAdd to the students array of the course IF the student does not exist (signified by user being a reference by objectId), and if the student already exists then I want to add the eventId (this._id) to the events array for that particular student and set status for that student to &quot;booked&quot;.</p>
  <p>Is this possible? I have tried many iterations using $cond, $elemmatch, &quot;students.$[]&quot; and so forth but I am quite new to mongodb and am unsure how to go about this.</p>