1

i am getting duplicate key error while doing the POST action with the same document that is already saved. i don't want it to be unique.

Error

MongoError: E11000 duplicate key error collection: myDB.users index: username _1 dup key: { : null }

> Schema
>     var userSchema = new Schema({
>       name: {type: String , required : true},
>       age : {type: String , required : true}
>     });

POST

router.post('/' , function(req, res, next){
 var newUser = new user(body);
 newUser.save(function(err) {
  if (err) throw err;
  res.sendStatus(200);
});
Haris KK
  • 267
  • 1
  • 4
  • 14
  • if you have unique index on fields , it throws duplicate key error, pls check your collection field indexes – radhakrishnan Apr 19 '17 at 14:33
  • you probably renamed username to name, and it sets null as default. Check this for more infos: http://stackoverflow.com/questions/38347186/mongoose-caused-by-11000-e11000-duplicate-key-error-index – Paolo Apr 19 '17 at 14:35
  • That was it, thank you. I have modified my mongoose schema, but didn't drop the collection. – Haris KK Apr 19 '17 at 14:47
  • Possible duplicate of [E11000 duplicate key error index in mongodb mongoose](https://stackoverflow.com/questions/24430220/e11000-duplicate-key-error-index-in-mongodb-mongoose) – faintsignal Jul 29 '18 at 15:29

0 Answers0