DropDups true does not work mongodb

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.

+6
source share
2 answers

The duplicate duplication feature when creating an index is no longer supported since Mongolian version 3.0. See the compatibility change page for release 3.0.

+11
source

This is a more invalid version of starting MongoDB 3.0.

Prior to MongoDB 3.0, MongoDB Unique + dupDrops functionality was used, as shown at http://dbversity.com/mongodb-dropdups-to-remove-duplicate-records/

0
source

Source: https://habr.com/ru/post/985514/


All Articles