8
votes

I have a simple "users" collection inside which right now i have only 2 documents.

{
    "_id": ObjectId("4ef8e1e41d41c87069000074"),
    "email_id": {
        "0": 109,
        "1": 101,
        "2": 64,
        "3": 97,

{
    "_id": ObjectId("4ef6d2641d41c83bdd000001"),
    "email_id": {
        "0": 109,
        "1": 97,
        "2": 105,
        "3": 108,

now if i try to create a new index with {unique: true} on email_id field, mongodb complaints me with "E11000 duplicate key error index: db.users.$email_id dup key: { : 46 }". I get same error even after specifying {dropDups: true}, however i don't think this is the case here, as both document have different email id's stored.

I am not sure what's going on here, any pointers will be greatly appreciated.

Edit: Full view of documents:

{
"_id": ObjectId("4ef8e1e41d41c87069000074"),
"email_id": {
 "0": 109,
 "1": 101,
 "2": 64,
 "3": 97,
 "4": 98,
 "5": 104,
 "6": 105,
 "7": 110,
 "8": 97,
 "9": 118,
 "10": 115,
 "11": 105,
 "12": 110,
 "13": 103,
 "14": 104,
 "15": 46,
 "16": 99,
 "17": 111,
 "18": 109
 }
}

and

{
"_id": ObjectId("4ef6d2641d41c83bdd000001"),
"email_id": {
 "0": 109,
 "1": 97,
 "2": 105,
 "3": 108,
 "4": 115,
 "5": 102,
 "6": 111,
 "7": 114,
 "8": 97,
 "9": 98,
 "10": 104,
 "11": 105,
 "12": 110,
 "13": 97,
 "14": 118,
 "15": 64,
 "16": 103,
 "17": 109,
 "18": 97,
 "19": 105,
 "20": 108,
 "21": 46,
 "22": 99,
 "23": 111,
 "24": 109
 }
}

There are a couple of more fields like "display_name", "registered_since", etc which i have omitted from the display above (i don't think they have any role in the error thrown, if you still need them i can probably paste the entire documents here)

I am using erlang mongodb driver for communication with my mongo instance. All fields as can be seen are saved as binary bytes, thats why you see such weird email_id in document.

Note: Binary byte format is not forced by my code logic, i very much pass string email_id inside my bson documents, but i always end up seeing my data as binary bytes. (Probably because how erlang mongodb driver is written, i didn't really investigate on this, since my find(), find_one() and other queries works as expected even with fields saved as binary bytes)

Edit: > db.users.findOne()

{
"_id" : ObjectId("4ef6d2641d41c83bdd000001"),
"email_id" : [
    109,
    97,
    105,
    108,
    115,
    102,
    111,
    114,
    97,
    98,
    104,
    105,
    110,
    97,
    118,
    64,
    103,
    109,
    97,
    105,
    108,
    46,
    99,
    111,
    109
],
"display_name" : [
    65,
    98,
    104,
    105,
    110,
    97,
    118,
    43,
    83,
    105,
    110,
    103,
    104
],
"provider" : [
    106,
    97,
    120,
    108,
    46,
    105,
    109
],
"provider_id" : [ ]
}
2
Why your email looks so strange? - Sergio Tulentsev
Are the values for email_id actually subdocuments indexed by string keys which are integers? Or are they arrays, and this is just the representation of them in your programming language? Also, which language are you using? - dcrosta
You need to provide more information. It appears the documents you provided are incomplete? Does 'email_id' have more than 3 elements? Perhaps element 46 is duplicated in the two documents? When/how are you adding an index? Is this in a script or in the mongo shell? As @Sergei Tulentsev says, you should provide a complete view of the documents. And the smallest set of code you can that reproduces the problem. - lhagemann
@chakram88 i tried creating my indexes are inserting two documents in the collection. I have tried creating indexes using both mongo shell and rockmongo admin panel with same error. I am not sure why really actually my email_id in documents show up as above. My bson document while inserting inside the collection is as simple as {email_id, "[email protected]}. - Abhinav Singh
Can you paste the output of db.users.findOne() from the mongo shell? I'm still not sure if Mongo is treating these as arrays or as subdocuments. - dcrosta

2 Answers

5
votes

When MongoDB indexes an array field, it actually indexes the individual elements in the array. This is to efficiently support queries looking for a particular element of an array, like:

db.users.find({email_id: 46})

Since this email_id (46) exists in both documents, there are duplicate keys in your unique index.

I'm not sure why you would get this error if you have dropDups: true set... can you show a code sample with how you're invoking createIndex? You should also try dropDups: 1, as MongoDB erroneously treats 1 and true differently in this context (see https://jira.mongodb.org/browse/SERVER-4562).

0
votes

For others having this problem, check your mongo version with db.version(). If you are running Mongo 3 and are trying to use dropDups to clear duplicates, it will fail and give you this error.