使用MongoDB存储在线教育应用程序的成绩。下面是我存储在mongoDB数据库中的classRoom文档。 StudentGradeObjs
存储在GradeObject
内的数组中。 GradeObjs
存储在教室内的GradeObjects
数组中。
将GradeObjs
视为" hw1"," assignment2"等。
和studentGradeObjs
一样,学生如何完成任务。
{
"_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
}
}
}
如何更新gradeValue
数组中的单个studentsGrades
属性?我的查询看起来像这样
classCollection.updateOne({$and: [{_id: ObjectId(classID)}, {"gradeObjs.gradeName": gradeName}]}, {$set: {"studentGrades.$.gradeValue": value}}, function(err, thing){
答案 0 :(得分:0)
我以不同的方式完成了这项工作。相反,我只是使用一些for循环来计算索引,然后使用找到的索引执行updateOne。