Search code examples
couchdbcloudant

How to view a specific document by key using curl


I have a couchdb database. I would like to retrieve the document that has the following key:value :

email:example@gmail.com

I am new to this. Should I create a 'design' document. If yes can you help ?

curl https://louisromain.cloudant.com/boardline_users/_view/byName?key="example@gmail.com"

Solution

  • Yes! Create a design document. You want to make a view which will index the email field of your documents. For example, in a design doc named _design/users, if you set .views.byEmail.map to this:

    function(doc) {
      if (doc.email) {
        emit(doc.email, doc);
      }
    }
    

    Then you can use curl:

    curl 'https://louisromain.cloudant.com/boardline_users/_design/users/_view/byEmail?key="example@gmail.com"'
    

    Note, I've single-quoted the entire URL. That way, the curl program will see the double-quotes around the email and it will encode them to send to Cloudant. (Otherwise, Bash would "eat" them and curl would send the wrong value to Cloudant.)