user2101699
user2101699

Reputation: 257

MongoDB - edit nested lists

I have a collection that looks like:

  {
    "key": 1,
    "assignments": [
        {
        "title" : "assignmend no 1",
        "students" : [1, 13, 4, 15]
    },{
        "title" : "assignmend no 2",
        "students" : [3, 66, 2, 13]
    },{
        "title" : "assignmend no 3",
        "students" : [12, 13]
    },{
        "title" : "assignmend no 3",
        "students" : [2]
    }
    ]
  }

The collection represent a class with multiple students and multiple assignments. Each student can be assigned to multiple assignments and each assignment can have multiple students.

I want to create a query that given an _id of the class and a student_id removes the student from each task it is assigned to.

How can I achive that?

Upvotes: 0

Views: 56

Answers (1)

Pritthish Nath
Pritthish Nath

Reputation: 134

Class.findOneAndUpdate({ _id: 'classID' }, { $pull: { 'assignments.students': 'studentID' }});

Let me know if this works

Upvotes: 1

Related Questions