数据结构
{
"_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
}
]
}
);
更新成功
网友评论