Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropDups true not working mongodb

Tags:

mongodb

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.

like image 753
inquisitive Avatar asked Apr 20 '15 11:04

inquisitive


1 Answers

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.

like image 63
Kai Sternad Avatar answered Nov 08 '22 11:11

Kai Sternad