我正在构建一个模拟的客户满意度应用程序。它将包括一个“问题和答案”部分,用户可以在其中单击“是”或“否”按钮以指示对特定问题的答案是否有帮助。我正在尝试编写可以增加此yes / no属性的PATCH方法,但是在访问该属性时遇到了麻烦。我的数据是一个对象数组,每个对象的格式如下:
{
"_id": "5f70eba3f66102b605e9b513",
"question_id": 7,
"product_id": 81,
"created_at": "2020-09-07T15:49:10.232Z",
"user": "Jerome_Rice12",
"question_body": "Et aliquam ea doloribus.",
"answers": [
{
"helpful": {
"yes": 0,
"no": 3
},
"_id": "5f70eba3f66102b605e9b514",
"answer_id": 2713,
"created_at": "2020-01-17T03:07:41.298Z",
"body": "Est voluptatum iure necessitatibus aut et.",
"user": "Travon.Gottlieb"
},
{ more answer objects },
],
"__v": 0
},
{ more question objects },
要访问“是”或“否”属性,我首先必须找到相关的问题ID,然后是正确的答案ID,然后进入并更新Answers.helpful.yes / no属性。迄今为止,每次尝试都因邮递员而失败。我尝试了以下多种不同格式的文件-到目前为止,没有任何工作。任何帮助将不胜感激。
const quesId = req.params.question_id;
const ansId = req.params.answer_id;
Questions.findOneAndUpdate({
question_id: quesId, answers: { answer_id: ansId } },
{ $inc: { 'answers.$.helpful.$.no': 1 } }).exec()
.then((data) => {
res.send(data);
});
答案 0 :(得分:0)
尝试一下:
Questions.findOneAndUpdate({
question_id: quesId,
'answers.answer_id': ansId
},
{
$inc: {
'answers.$.helpful.no': 1
}
})