从节点 js 更新 mongodb 中的值
Update values in mongodb from node js
下面是我尝试更新 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');
});
});
如何纠正错误?
任何帮助将不胜感激。
检查你的错误,它清楚地指出
duplicate key error index:
这意味着您正在尝试复制一个索引(例如,有两次),但您不能这样做。
我再次检查了您的代码,发现您有
app.get('/feature'
两次。有什么原因吗?
PS: MongoDB说你有两次相同的ID,这可能意味着你的前端有问题(后端似乎没问题),你发送的是相同的值。
编辑:如果你想更新记录,你必须使用猫鼬的更新方法。这是文档必须要说的 http://mongoosejs.com/docs/2.7.x/docs/updating-documents.html
否则,您只是在尝试使用相同的数据创建一条新记录。
请避免使用现有 _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!' });
});
});
});
@拉贾
如何更改编辑按钮,以便在单击它时应该 select ID。
我在 click.Below 上打开页面的 link 编辑选项是代码:
<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>
下面是我尝试更新 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');
});
});
如何纠正错误? 任何帮助将不胜感激。
检查你的错误,它清楚地指出
duplicate key error index:
这意味着您正在尝试复制一个索引(例如,有两次),但您不能这样做。
我再次检查了您的代码,发现您有
app.get('/feature'
两次。有什么原因吗?
PS: MongoDB说你有两次相同的ID,这可能意味着你的前端有问题(后端似乎没问题),你发送的是相同的值。
编辑:如果你想更新记录,你必须使用猫鼬的更新方法。这是文档必须要说的 http://mongoosejs.com/docs/2.7.x/docs/updating-documents.html 否则,您只是在尝试使用相同的数据创建一条新记录。
请避免使用现有 _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!' });
});
});
});
@拉贾 如何更改编辑按钮,以便在单击它时应该 select ID。
我在 click.Below 上打开页面的 link 编辑选项是代码:
<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>