0

Using MongoDB to store grades for online eduction application. Below is a classRoom document I am storing in my mongoDB database. StudentGradeObjs are stored in an array inside of a GradeObject. GradeObjs are stored in an array of GradeObjects inside a classroom.

Think of GradeObjs as "hw1", "assignment2", etc. And a studentGradeObjs as how a student did on an assignment.

{
"_id" : ObjectId("5a01d9e3a91726026cb2e52b"),
"name" : "Bio 212",
"teacherUsername" : "Mike",
"studentArray" : [ 
    "5a00cfd4ae1c8a0114d61095", 
    "59ff75021365601935b674d0", 
    "59ff6282fe19e01fdcc2f505"
],
"gradeObjs" : [ 
    {
        "gradeName" : "hw1",
        "gradeValueMax" : 20,
        "dueDate" : "12/12/12",
        "studentGrades" : [ 
            {
                "username" : "james",
                "gradeValue" : null,
                "submissionDate" : null,
                "isSubmitted" : false
            }, 
            {
                "username" : "DickButt",
                "gradeValue" : null,
                "submissionDate" : null,
                "isSubmitted" : false
            }, 
            {
                "username" : "john",
                "gradeValue" : null,
                "submissionDate" : null,
                "isSubmitted" : false
            }
        ]
    }, 
    {
        "gradeName" : "hw2",
        "gradeValueMax" : 30,
        "dueDate" : "4/4/15",
        "studentGrades" : [ 
            {
                "username" : "james",
                "gradeValue" : null,
                "submissionDate" : null,
                "isSubmitted" : false
            }, 
            {
                "username" : "john",
                "gradeValue" : null,
                "submissionDate" : null,
                "isSubmitted" : false
            }, 
            {
                "username" : "DickButt",
                "gradeValue" : null,
                "submissionDate" : null,
                "isSubmitted" : false
            }
        ]
    }
],
"studentGrades" : {
    "gradeValue" : 69,
    "1" : {
        "gradeValue" : 69
    }
}

}

How can I update an individual gradeValue property inside the studentsGrades array? My query looks something like this

classCollection.updateOne({$and: [{_id: ObjectId(classID)}, {"gradeObjs.gradeName": gradeName}]}, {$set: {"studentGrades.$.gradeValue": value}}, function(err, thing){
antzshrek
  • 9,276
  • 5
  • 26
  • 43
FortunateSonn
  • 11
  • 1
  • 2

1 Answers1

0

I got this work a different way. Instead, I just used some for loops to figure out the indexes and then did updateOne with the found indexes.

FortunateSonn
  • 11
  • 1
  • 2