美文网首页
[mongoose] "Too many positional

[mongoose] "Too many positional

作者: w_w_wei | 来源:发表于2020-06-19 11:07 被阅读0次

    数据结构

    {
            "_id": "5ee9db02738e0e8d50943b30",
            "result": [{
                "record_results": [{
                    "video": ["http://www.baidu.com"],
                    "_id": "5ee9db7d738e0e8d50943b36",
                    "name": "场景测试1"
                }],
                "_id": "5ee9db7d738e0e8d50943b35",
                "user_name": "24",
                "user_id": "5ee9db0e738e0e8d50943b31",
                "appointment_id": "5ee9db27738e0e8d50943b32"
            }]
        }
    

    如上数据结构, 需要更新 video
    报错代码

    const result = await ctx.model.Project.Project.findByIdAndUpdate(
                    { 
                        _id: ctx.params.id, 
                        'result.name': ctx.params.user_id,
                        'result.record_results.name':  ctx.request.body.sence_name
                    {
                        $set: {
                            'result.$.record_results.$.video': video
                        }
                    }
                );
    

    错误

    ok: 0
    errmsg: "Too many positional (i.e. '$') elements found in path 'result.$.record_results.$.video'"
    code: 2
    codeName: "BadValue"
    name: "MongoError"
    

    解决方案, 使用 arrayFilters

                const result = await ctx.model.Project.Project.findByIdAndUpdate(
                    ctx.params.id,
                    {
                        $set: {
                            'result.$[idx0].record_results.$[idx1].video': video
                        }
                    },
                    {
                        arrayFilters: [
                            {
                                'idx0.user_id': ctx.params.user_id
                            },
                            {
                                'idx1.name': ctx.request.body.sence_name
                            }
                        ]
                    }
                );
    

    更新成功

    相关文章

      网友评论

          本文标题:[mongoose] "Too many positional

          本文链接:https://www.haomeiwen.com/subject/rpspxktx.html