问题
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:
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.
来源:https://stackoverflow.com/questions/41499611/mongodb-text-index-duplicate-key-error-when-part-of-string-field-same