Unique constraint with document lookup and counter question

Hi all,

I know that the general way to model a unique constraint/unique index concept is to use document lookup. I have a few questions related to this approach:

  1. Using the example found here1, when processing/trying to insert a parent document, the e-mail could be check first with a lookup (KV). Won’t that technically create a race condition because there is now 2 operations (e.g., KV-look, if that does not find anything then insert the document) since after the KV-lookup, maybe the e-mail got added? Alternatively, instead of a lookup, it is to directly insert that smaller document for the e-mail address and see if it fails. This is two operations, which also still opens the door for a race condition?

  2. If I need uniqueness across more than one attribute, but there are business logic that prevents me from combining them together like the one posted here2. I don’t really understand how a counter would work for this because it was noted by the OP that the uniqueness is really alias OR loginName.

Thank you.
Regards, Steve

Anybody can help shed some light? :sunglasses:

Apologies that this got lost in the shuffle and we never responded.

The guidance provided in Can i do unique constraint on some attribute not the id would be the best approach.

There is no race condition because you’re not doing a lookup first…you’re doing the insert first which is atomic. If the insert fails, you know that a document with that key already exists.