You can use the e-mail address as an id of the document, so no other document with the same e-mail can be added. You can also use the essential feature of the CouchDB, the view, to check if e-mail exists before insertion. Both methods require later conflict resolution. If first case, there may be another Couch node with conflicting document that has yet to be replicated. In second case, two users may add the accounts to the same server, but both will first check for existing e-mails, and then both will add their accounts.
Adding view requires adding the design document where the definition of calculated view is. You can create an index with the map function like this:
function (doc) {
if (!doc.email) return;
emit(doc.email, doc);
}
You can query the view with:
http://localhost:5984/YOUR_DTABASE/_design/YOUR_DESIGN_DOCUMENT/_view/YOUR_VIEW?key='[email protected]'
Note, that e-mail comparison is not that trivial as you could expect:
- DNS domain name (after "@") is case insensitive (user name is usually case sensitive)
- In typical e-mail system you can assign aliases.
- Some e-mail systems allow to embed destination folders and additional information that changes the e-mail string but directs to the same box (e.g. [email protected]).
- Gmail ignores ".", so [email protected] equals [email protected]