I am using mongoDB shell with version 3.0.2 I am trying to ensure uniqueness restriction over the username field in the users collection.
This is what I gave:
db.users.ensureIndex({"username": 1},{unique: true})
This gave me the following error:
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "errmsg" : "exception: E11000 duplicate key error index: mybackend.users.$username_1 dup key: { : \"rahat\" }", "code" : 11000, "ok" : 0 }
Then I used dropDups: true in the command:
db.users.ensureIndex({"username": 1},{unique: true, dropDups: true})
Then I also get the same error:
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "errmsg" : "exception: E11000 duplicate key error index: mybackend.users.$username_1 dup key: { : \"rahat\" }", "code" : 11000, "ok" : 0 }
I also saw this SO link, but here it already had one index. Mine doesnβt have one.
db.users.getIndexes() β
[ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mybackend.users" } ]
I searched for this problem on github and restarted mongo but didn't use it. What am I doing wrong? I think I'm making a stupid mistake. Please help.