What is the best way to solve this problem with couchbase?

I have big amounts of documents and I need to assign to them certain codes dynamically (by this I mean not all at once, but any code at any given time). These codes keep a specific format and must be unique for every document.

My question is: Is it better to make a view that emits all the codes from all the documents and check said view before assigning the document (maybe even on a sync function) to see if the code is already in use, or is it better to create documents with the code as an id and a reference to the document they have been assigned to, and just check if the document already exists?

The latter option sounds faster since we are talking about a lot of documents (in the hundreds of thousands potentially) and a view would have a bad time keeping up with the updates. On the other hand, potentially doubling up the amount of documents doesn’t seem appealing either.

I also have a third option which consists of having special documents with an assigned range of codes, which would contain a map with all the assigned codes within their range and the documents they have been assigned to. This however seems like it would make for a lot of conflicts as many users would access those documents simultaneously.

Hi @cilveti. How about this:

  1. As a one-off write all your codes into a single special document.
  2. Also initialise an atomic counter document.
  3. Each time you need to create a doc, get the next value from the counter, and use it to index into the codes document.

You could also reduce the contention on the counter document if you created say 20 counter/code-doc pairs, each with a unique subset of the codes, and randomly pick one each time you create a doc.

Would that work?