我想我知道如何找到我的对象,我只是无法弄清楚如何更新它的价值观。我收到了这个错误:
{
"name": "MongoError",
"message": "cannot use the part (tasks of families.25.tasks.name) to traverse the element ({tasks: [ { name: \"Correct Name\", isSelected: false, completedBy: null, submittedBy: \"web-user\", completedOn: null, submittedOn: new Date(1505857352113), message: \"Please correct family name to follow HOK standard.\", assignedTo: \"konrad.sobon\", _id: ObjectId('59c18f8991d1929d43f22f8c') }, { name: \"Some other task\", isSelected: false, completedBy: null, submittedBy: \"web-user\", completedOn: null, submittedOn: new Date(1505917405948), message: \"Yet again, testing this.\", assignedTo: \"konrad.sobon\", _id: ObjectId('59c279fb8388cb58e7454bf6') } ]})",
"driver": true,
"index": 0,
"code": 16837,
"errmsg": "cannot use the part (tasks of families.25.tasks.name) to traverse the element ({tasks: [ { name: \"Correct Name\", isSelected: false, completedBy: null, submittedBy: \"web-user\", completedOn: null, submittedOn: new Date(1505857352113), message: \"Please correct family name to follow HOK standard.\", assignedTo: \"konrad.sobon\", _id: ObjectId('59c18f8991d1929d43f22f8c') }, { name: \"Some other task\", isSelected: false, completedBy: null, submittedBy: \"web-user\", completedOn: null, submittedOn: new Date(1505917405948), message: \"Yet again, testing this.\", assignedTo: \"konrad.sobon\", _id: ObjectId('59c279fb8388cb58e7454bf6') } ]})"
}
我的收藏如下:
我的请求处理程序如下所示:
module.exports.updateTask = function (req, res) {
var id = req.params.id;
var taskId = mongoose.Types.ObjectId(req.params.taskid);
Families
.update(
{ _id: id, 'families.tasks._id': taskId},
{ $set: {
'families.$.tasks.name': req.body.name,
'families.$.tasks.message': req.body.message,
'families.$.tasks.assignedTo': req.body.assignedTo}}, function(err, result){
if(err) {
res
.status(400)
.json(err);
} else {
res
.status(202)
.json(result);
}
}
)
};
任何帮助将不胜感激。
答案 0 :(得分:1)
不幸的是,位置运算符 $ 不能用于嵌套数组:
位置$运算符不能用于遍历多个数组的查询,例如遍历嵌套在其他数组中的数组的查询,因为$ placeholder的替换是单个值。
https://docs.mongodb.com/manual/reference/operator/update/positional/
您可能需要考虑重组文档。