首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新嵌套数组中的元素

更新嵌套数组中的元素
EN

Stack Overflow用户
提问于 2017-09-20 15:07:08
回答 1查看 42关注 0票数 0

我想我知道如何到达我的对象,我只是不知道如何更新它的值。我得到了这个错误:

代码语言:javascript
复制
{
    "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') } ]})"
}

我的收藏品是这样的:

我的请求处理程序如下所示:

代码语言:javascript
复制
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);
                }
            }
        )
};

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-20 15:35:25

不幸的是,位置运算符$不能用于嵌套数组:

位置$运算符不能用于遍历多个数组的查询,例如遍历嵌套在其他数组中的查询,因为$占位符的替换是一个值。

https://docs.mongodb.com/manual/reference/operator/update/positional/

你可能得考虑重组你的文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46325658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档