使用mongoose和axios在mongodb中更新数组

我有一个项目,可以在一个审核中添加尽可能多的设备。从我的前端开始,我已经有一种方法可以发送具有相同标题的每个设备审核。但是,我发送的每个新文档总是在创建一个新文档,而不是加入第一个插入的具有相同标题名称的文档。

在我的模型中,我有一个名为devices的数组。该设备数组旨在在保持相同标题的同时获取尽可能多的对象。我尝试使用Upsert方法,$ set,并且两者都无法正常工作,或者我不知道该如何处理。以下是我的model.js和我尝试使用的上一个查询...

const mongoose = require("mongoose");
```// Defining a model
mongoose.model("Audit",{
    title: String,devices:
        [
          {
          device: String,rating: String,quantity: String,operation: String
          }               
        ],date: {
        type: Date,default: Date.now
    }
})```


```app.put("/audits",(req,res) => {
    var newInput = {
      title: req.body.title,devices: req.body.devices,device: req.body.device,rating: req.body.rating,quantity: req.body.quantity,operation: req.body.operation,}
var query = {'title':req.body.title};
req.newData.title = req.body.title;
model.findOneAndUpdate(query,req.newData,{upsert:true},function(err,doc){
    if (err) return res.send(500,{ error: err })
    return res.send("succesfully saved")
})
})```
wyq_1234 回答:使用mongoose和axios在mongodb中更新数组

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3162932.html

大家都在问