C
C
coriolis19862017-08-04 22:49:07
MongoDB
coriolis1986, 2017-08-04 22:49:07

Why am I getting a uniqueness violation issue in MongoDB?

There was a 1-in-1 situation like here - https://stackoverflow.com/questions/41499611/mongo...
What did the person in the comments mean when he wrote that he found the solution:

do not use one index but two indexes
One for text index, one for name only

How are such indexes created?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2017-08-04
@zoonman

Because MongoDB, when building a text index, breaks the string into words, then removes the so-called stop words and can normalize them.
Let's say you have the following 2 documents.

{text: "Два красивых яблока" }
{text: "Много яблок" }

When the text index is formed, the following will happen. In fact, everything happens a little differently, in general, the process is as follows.
The string is tokenized into words:
"Два красивых яблока" → ["Два", "красивых", "яблока"]
"Много яблок" →  ["Много", "яблок"]

Further words are normalized
["Два", "красивых", "яблока"] → ["два", "красивый", "яблоко"]
["Много", "яблок"] →  ["много", "яблоко"]

Now the vocabulary building begins. We start assigning numbers to each word.
["два", "красивый", "яблоко"] → [1, 2, 3], и словарь [1↔"два", 2↔"красивый", 3↔"яблоко"]
["много", "яблоко"]  →  [4, 3], и в словарь [1↔"два", 2↔"красивый", 3↔"яблоко", 4↔"много"]

Those. get the next set in the index
[1→1, 2→1, 3→1]
[4→2, 3→2]

Because is an index, then the partitions are merged into a common pointer
1→1, 
2→1, 
3→1, 
4→2 
3→2

And now look, our index is declared unique, but the pointers to the records are repeated, which means the pointer is not unique.
Therefore, an error occurs.
To solve the problem, you need to create 2 indexes.
The first one is text, but without the uniqueness requirement.
The second one is unique, based on a binary tree.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question