I have in my User collection an field array called emails where I added an mva unique constraint. As my understanding about mva() in the context of unique that it creates a separate index entry for each value in the array so it should avoid having the same string multiple times. Leading to, that this should not work:
emails: [
"john@doe.com",
"john@doe.com"
]
I expected to get a unique constraint error, but surprisingly this is working. What am I missing here?
Ok, so the unique constraint is only working between different documents, but not the same document.
So Example 1 and 2 will throw an error, but 3 won’t. That means, for example 3 we need your mentioned workaround.
Is that intended, or do you plan to fix this behavior so that the unique constraint also works in the same document array?
Hi! Sorry for not being more precise before. The behavior is expected: that your Example 3 will NOT throw a constraint failure for the unique [.emails] configuration. There are no plans to change this behavior. An explicit check constraint is the recommended method to enforce that all items in your array are distinct.