I am using mongoDB shell with version 3.0.2
I am trying to ensure uniqueness constraint over a username
field in collection users
.
This is what I gave:
db.users.ensureIndex({"username": 1},{unique: true})
It gave me 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 too I 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 does not have one.
db.users.getIndexes()
->
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mybackend.users"
}
]
I looked for this issue on github and restarted the mongo but to no use. What am I doing wrong? I think I am doing a silly mistake. Please help.
The drop duplicates functionality on index creation is no longer supported since Mongo version 3.0. See the compatibility changes page for the 3.0 release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With