这是我的 JSON 响应:
"_id" : 537,
"quizzes" : [
{
"wk" : 1,
"score" : [
10
]
},
{
"wk" : 2,
"score" : [
8
]
},
{
"wk" : 3,
"score" : [
5
]
},
{
"wk" : 4,
"score" : [
6
]
}
]
}
我正在尝试更新其中一个对象内的分数数组,这是我的尝试:
db.collection('connect').update({_id: id}, {$push: { quizzes[0]: { score: 89 } }});
最佳答案
尝试以下更新:
db.collection("connect").update(
{
"_id": id
},
{
"$set": {
"quizzes.0.score.0": 89
}
}
);
它使用 dot notation 访问数组的元素以及访问嵌入文档的字段。
To access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zero-based index position, and enclose in quotes:
'<array>.<index>'
关于javascript - JSON 更新对象内部的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30959223/