当在同一文档上多次触发时,mongoose findOneAndUpdate无法更新

发布时间:2020-07-07 12:38

当我连续发送多个请求时,它们之间的时间间隔很短,以猫鼬中的findOneAndUpdate更新同一文档时,某些更改未应用。 我相信这是因为findOneAndUpdate不会检查版本,而只是覆盖文档。但是我不能解决这个问题。 这是我的处理程序:

app.post('/updatetrialsession',authenticateJWT ,(req,res)=>{        
    User.findOneAndUpdate({
        username: req.user.username
      }, {
           $set: {'examTrials.$[elem1].questions.$[elem2].answer' : req.body.question.answer,
                'examTrials.$[elem1].questions.$[elem2].marked' : req.body.question.marked,
                'examTrials.$[elem1].questions.$[elem2].tried' : true,
                'examTrials.$[elem1].currentQuestion':req.body.questionIdx+req.body.incrementCounter,
                'examTrials.$[elem1].numOfSolved': req.body.numOfSolved,
                'examTrials.$[elem1].countDown' : req.body.countDown}
      }, {
           arrayFilters: [{'elem1.trialId': req.body.trialId }, {'elem2.id': req.body.questionIdx}]
      }).then(res=>{
          console.log("updated")
          
      })
})

假设一系列要求不同的问题,但它们都在同一考试中,因此文件也相同。 当我发送请求的时间间隔为2秒时,它就可以正常工作了。 有人可以帮忙吗?

回答1