node.js - mongoose allow duplicate fields while saving -
this question has answer here:
- mongoosejs cant disable unique field 1 answer
my user model looks like:
var userschema = new schema({ name: { type: string, unique: false }, user_url:{ type: string }, email: { type: string, lowercase: true }, role: { type: string, default: 'individual' }, plan: { type: string, default: 'basic' }, password: string, provider: string, salt: string, facebook: {}, twitter: {}, google: {}, github: {} });
as can see, trying allow records duplicate value in name
fields. when trying save throws error:
unhandled rejection writeerror({"code":11000,"index":0,"errmsg":"e11000 duplicate key error index: dev.users.$name_1 dup key: { : \"ttp2\" }","op":{"user_url":"ttp2-0","salt":"ly59ooaz+8zz4z8+da/7dw==","provider":"local","name":"ttp2","email":"ttp2_1@example.com","password":"weoc+pejewfz/s11swqimphxrkh7ylc+q+oa6tad/qgvh+oftbmasvssbrvukorje1dyjkzeh5uvcejsgxahna==","_id":"56b5cc49b6bc872413f6c9fb","__v":0}})
what doing wrong?
presumably, there still unique index on database. if remove unique constraint mongoose schema has been defined before, not reflect on database. remove index directly via mongodb client.
you can update schema programmatically, putting model.js:
mongoosemodel.collection.dropindex('name_of_your_index');
Comments
Post a Comment