couchdbcloudantcouchdb-mango

How to check for duplication before creating a new document in CouchDB/Cloudant?


We want to check if a document already exists in the database with the same fields and values of a new object we are trying to save to prevent duplicated item.

Note: This question is not about updating documents or about duplicated document IDs, we only check the data to prevent saving a new document with the same data of an existing one.

Preferably we'd like to accomplish this with Mango/Cloudant queries and not rely on views.

The idea so far is:

1) Scan the the data that we are trying to save and dynamically create a selector that matches that document's structure. (We can't have the selectors hardcoded because we have types of many documents)

2) Query de DB with for any documents matching that selector to if any document already exists that matches those criteria.

However I wonder about the performance of this approach since many of the selector fields will not be indexed.

I also much rather follow best practices than create something out of the blue, but haven't been able to find any known solutions for this specific scenario.

If you happen to know of any, please share.


Solution

  • Option 1 - Define a meaningful ID for your documents

    The ID could be a logical coposition or a computed hash from the values that should be unique

    If you want to check if a document ID already exists you can use the HEAD method

    HEAD /db/docId

    which returns 200-OK if the docId exits on the database.

    If you would like to check if you have the same content in the new document and in the previous one, you may use the Validate Document Update Function which allows to compare both documents.

    function(newDoc, oldDoc, userCtx, secObj) {
    ...
    }
    

    Option 2 - Use content hash computed outside CouchDB

    Option 3 - Compute hash in a View