【发布时间】: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