PB mongodb,mongoose,node.js
当我尝试在mongodb中插入某些东西时,出现这个错误,您能否给我一个理解的文档,并做我需要的东西?
/home/samp/Bureau/Node/gw/gw-user/node_modules/mongoose/lib/utils.js:413 throw err; ^ MongoError: E11000 duplicate key error index: mongoose-bcrypt-test.users.$username_1 dup key: { : "samp@hotmail.com" }
这是我的代码:
user.save(function(err) { if (err) throw err;
重复的键错误意味着你正试图插入一个已经存在于MongoDB中的值(看起来像samp@hotmail.com)作为唯一键。 使用另一个用户值。
如果你正在添加的值已经存在,那么你会得到这个错误。 为了简单,我使用了_id字段。
> db.test.insert({_id:123,Name:"test1"}) > > db.test.insert({_id:123,Name:"test2"}) E11000 duplicate key error index: test.test.$_id_ dup key: { : 123.0 } >