0

I am quiet new to mongoose, So i created a user model like this

var UserSchema = new mongoose.Schema({
    name: { 
        type: String,
        lowercase:true,
        unique:true
    },
    phone: {
        type:String,
        unique:true,
        sparse:true,
        required:true
    }});
mongoose.model('User',UserSchema);

I created this model, but later on i thought that name field should not be unique so i updated my model by removing unique attribute from name field. but is mongoose keeping some cache or something because even after updating the code in my model file when i do a user register request i am getting this error that duplicate key for name field and it is not accepting same names for 2 documents.

this is the error i am recieving :

{ [MongoError: E11000 duplicate key error collection: kiotapp.users index: name_1 dup key: { : "arihant daga 5" }] name: 'MongoError',
message: 'E11000 duplicate key error collection: kiotapp.users index: name_1 dup key: { : "arihant daga 5" }',

Daga Arihant
  • 464
  • 4
  • 19
  • Have a look at http://stackoverflow.com/questions/12337388/mongodb-remove-unique-constraint – hyades Nov 14 '16 at 06:09
  • Thank you @hyades it worked fine with db.collections.dropIndexes() but now i ran into another problem, if i drop these indexes and later on when i try to set unique attribute again, mongoose is not creating indexes again automatically. can u help with that ?? – Daga Arihant Nov 14 '16 at 06:35

1 Answers1

1
db.collection.dropIndex()

refer : https://docs.mongodb.com/v3.0/tutorial/remove-indexes/

Simran
  • 2,364
  • 1
  • 12
  • 19