0

Is it possible to retrieve a document, based on an attribute of that document?

I want to be able to test my users on sign up if the email already exists or not? It seems to me however, that you can only retrieve documents by _id...

I am using Cloudant as a cloud-service which uses CouchDB.

1
  • P.S, I realise there is a way of getting all of the documents in the database, but I don't want to get all of the documents and cycle through them if possible. Commented Nov 4, 2012 at 13:07

1 Answer 1

2

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:

  1. DNS domain name (after "@") is case insensitive (user name is usually case sensitive)
  2. In typical e-mail system you can assign aliases.
  3. 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]).
  4. Gmail ignores ".", so [email protected] equals [email protected]
Sign up to request clarification or add additional context in comments.

6 Comments

OK, it seems I need to use a JQuery library - this may sound stupid but I cannot find one anywhere - do you have a direct download link?
You can download jQuery from its home page. Is it somehow connected with this question about CouchDB, because I do not know how jQuery can help you with querying the database?
Hmm, I think I've confused myself. When using a map function is it true that to do so you store a document onto the database using a format similar to this: { "_id": "_design/application", "_rev": "1-C1687D17", "views": { "viewname": { "map": "function(doc) { ... }", "reduce": "function(keys, values) { ... }" } } }, then read it from the address: /database/_design/application/_view/viewname
Essentially the view is like a SQL view. It calculates the "table" from the other "table". You create the design document with some functions. The function map is run for each document in the DB. What it emits is a part of the new index. Using this index is very fast, but it has fewer capabilities then SQL view. The map result may also be the source for data aggregation with reduce. Other functions allow manipulation of the resulting format, partial document updates and also some data verification. They are the essence of the CouchDB. Take a look at The Book
OK, so I went back to the drawing board on this and have read in more detail how CouchDB works, I have a simple system which works well following the criteria you listed for email comparison. Do you know if there is a list of other criteria being used on emails?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.