1
votes

For examples:

doc1:
{
'name':'apple'
}
doc2:
{
'name':'apple juice'
}

when I create text index with pymongo:

db.products_collection.create_index([('name', TEXT)],
                                     unique=True,
                                    background=True)

it give me an error:

E11000 duplicate key error collection: c.items_collection index: name_text_alias_text dup key: { : "apple", : 10.5 }

Some one know why? I cannot add unique=True for text string?

1
apple and apple juice are two different values. They can not cause your error, you should check your db for any occurrenceskkkkkkk
I know what you mean. What the problems is the text index. I think mongodb use ' ' to split string to index to improve search and make this error happen. You can try with the text index.tim
I think I got a solution.tim
How do you solve it?kkkkkkk
@Khang do not use one index but two indexes.tim

1 Answers

2
votes

A text index splits strings into tokens (words), and those tokens form the keys. So in your example, "apple" is a duplicates key.

Generally, unique text index is the bad idea.