使用数组中的值更新猫鼬中的子文档

时间:2021-01-05 11:59:01

标签: arrays mongoose findoneandupdate

我已经在这里检查了所有类似的问题,但到目前为止还没有找到解决方案。我有以下架构

const scheduleSchema = new mongoose.Schema({
  lab:{
      type:mongoose.Schema.Types.ObjectId,
      ref:'Lab'
    },
  analyses:[{
    _id:false,
    analysis:String,
    analysisId:{
      type:mongoose.Schema.Types.ObjectId,
      ref:'Analysis'
    },
    value:mongoose.Decimal128,
  }],
})

我有一个表单,其中每个分析值都有输入字段,我想在此处为每个分析输入值,然后更新数据库中的值。

这是我为每个分析赋值的表格

enter image description here

这是我正在尝试的代码

exports.analysisInterpretation = async (req,res) => {
  const updateInterpretation = await Schedule.findOneAndUpdate(
    {_id:req.params.id},
    {$set:{'analyses.$[].value':req.body.value}},
    {
      new:true,
      runValidators:true,
      useFindAndModify:false
    }).exec()
}

但是使用此代码我收到错误

CastError: Cast to Decimal128 failed for value "[ '32', '3', '3', '3', '3' ]" at pa
th "value"

如何从数组中获取值并使用数组中的第一个项目更新第一个分析值,第二个使用第二个值等.

0 个答案:

没有答案