【问题标题】:Update values in mongodb from node js从节点 js 更新 mongodb 中的值
【发布时间】:2015-09-10 15:02:04
【问题描述】:

下面是我尝试更新 mongoDB 中的值的代码,但在我尝试更新值时它不起作用并显示错误。 即将出现的错误是:

{
    "name": "MongoError",
    "message": "insertDocument :: caused by :: 11000 E11000 duplicate key error index: agastyaforonewaydevelopmentstaging.deviceinfos.$_id_  dup key: { : \"355537056685953\" }",
    "index": 0,
    "code": 11000,
    "errmsg": "insertDocument :: caused by :: 11000 E11000 duplicate key error index: agastyaforonewaydevelopmentstaging.deviceinfos.$_id_  dup key: { : \"355537056685953\" }"
}

代码是:

 // mongoose.connect('mongodb://user:password@ds031671.mongolab.com:31671/oneway', function(err) {
if (err) {
    console.log('connection error', err);
} else {
    console.log('connection successful');
}
});

var deviceSchema = new mongoose.Schema({
    _id: {
    type: String,
    unique: true
    },
    MobileNum: Number,
    Fecode: String
})

var deviceinfo = mongoose.model('deviceinfo', deviceSchema);

// Device collection close

app.get('/feature', function(req, res) {
    res.render('feature.ejs', {
    user: req.user
    });
});
app.get('/feature', function(req, res) {

    res.render('feature.ejs');
});
app.post('/feature', function(req, res) {
    // if(collection.findOne({_id:req.body.Imei}))
    new deviceinfo({
    _id: req.body.Imei,
    MobileNum: req.body.Mobile,
    Fecode: req.body.fecode
    }).save(function(err, drd) {
    if (err) res.json(err);
    else
        res.render('feature.ejs');
    });
});

app.get('/fupdate', function(req, res) {

    res.render('upfeature.ejs');
});

app.post('/fupdate', function(req, res) {
    // if(collection.findOne({_id:req.body.Imei}))
    new deviceinfo({
    _id: req.body.Imei,
    MobileNum: req.body.Mobile,
    Fecode: req.body.fecode
    }).update(function(err, drd) {
    if (err) res.json(err);
    else
        res.render('feature.ejs');
    });
});

如何纠正错误? 任何帮助将不胜感激。

【问题讨论】:

    标签: node.js mongodb mongoose ejs


    【解决方案1】:

    @Raja 如何更改编辑按钮,以便在单击该按钮时应选择 id。

    我给出的编辑选项作为点击打开页面的链接。下面是代码:

         <table class="table table-bordered"> 
         <tbody>
    
             <tr>
                  <td><%= drd._id %></td>
        <td><%= drd.MobileNum %></td>
              <td><%= drd.Fecode %></td>
        <td> <a class="button" href="/fupdate"><img src="/images/edit.png" width="20" height="20"></a></td>
          </tr>
    </tbody>
    
    </table>
    

    【讨论】:

      【解决方案2】:

      请避免使用现有_id的新对象模型更新文档

      相反,您可以通过将 _id 作为查询字符串传递来更新文档

      app.post('/fupdate/:Imei', function(req, res) {
          deviceinfo.findById(req.params.Imei, function(err, device) {
      
              if (err)
                  res.send(err);
              device.MobileNum = req.body.Mobile;
              device.Fecode = req.body.fecode;
              device.save(function(err) {
                  if (err)
                      res.send(err);
      
                  res.json({ message: 'device updated!' });
              });
      
          });
        });
      

      【讨论】:

      • 感谢您的回复,但现在我还需要更改编辑按钮,以便在单击按钮时应选择 ID。
      • 将您的按钮 href attr 值更改为“/fupdate/”
      • 您是在点击更新按钮时遇到同样的错误还是其他错误
      • 显示路径不能为空的错误。您是否可以分享您的电子邮件 ID,以便我可以将整个代码发送给您
      【解决方案3】:

      检查你的错误,它清楚地说明了

      duplicate key error index: 
      

      这意味着您正在尝试复制索引(例如复制两次),但您不能复制。

      我再次检查了你的代码,我注意到你有

      app.get('/feature'
      

      两次。有什么原因吗?

      PS:MongoDB 说你有两次相同的 ID,这可能意味着你的前端有问题(后端似乎很好),而你发送的是相同的值。

      编辑:如果你想更新一条记录,你必须使用猫鼬的更新方法。这是文档必须要说的http://mongoosejs.com/docs/2.7.x/docs/updating-documents.html 否则,您只是尝试使用相同的数据创建新记录。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-20
        • 2021-11-11
        • 2014-08-30
        • 2018-06-15
        相关资源
        最近更新 更多