Mongodb text index Duplicate Key Error when part of string field same

我们两清 提交于 2020-02-05 08:46:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!