我想更新我的quiz_score
,如果qnNumber
不存在,我想将qnNumber
作为键,selectedAnsNum
作为值,如果它存在,我想要只是更新值
let quiz_score = {}
quiz_score[qnNumber] = selectedAnsNum
Meteor.users.update({
_id: Meteor.userId(),
"course_learn_list.course_id": courseId
}, {
$addToSet: {
"course_learn_list.$.quiz_score": {
quiz_score
}
}
})
答案 0 :(得分:2)
我不熟悉meteor
,这里只是mongo shell下的示例代码。为了满足您的要求,我认为可以通过以下两个命令来完成。你可以把它们转换成流星命令。
// if we find the match course, update this one through `$set`
db.users.update({_id: id, 'course_learn_list.course_id': courseId},
{$set: {'course_learn_list.$': {course_id: courseId, quiz_score: quiz_score}}});
// if we cannot find match course, insert this new one through `$addToSet`
db.users.update({_id: id, 'course_learn_list.course_id': {$ne: courseId}}, {
{$addToSet: {course_learn_list: {course_id: courseId, quiz_score: quiz_score}}});