varun Kishnani
varun Kishnani

Reputation: 169

Couch DB - Passing input parameters to view

I am moving from SQL to Couch DB from my web application, my very first application.

While i can not say why I do not like SQL queries, not sure that i don not, the idea of making CURL requests to access my database sound must better than using PHPs PDO .

I have spent a little over a day and a half trying to acquaint myself with the couch DB HTTP API. I can not claim I have throughly read the API , but who thoroughly reads an API before beginning to code. So my, possibly silly, question is - how do I pass an variable other than doc to a map function while making a http request to the view. The API clearly says that a map function only takes a single parameter which is "doc", in which case the function below itself is wrong but I can't find any section in the API that lets me query a database using end-user provided input.

my map function is

function(doc, pid2){

      if (doc.pid === pid2)
    {
        emit(doc._id, doc) ;

    }
}

pid2 is a number that will be provided by a front end user.

    <?php
    $pid2 = file_get_contents(facebook graphi api call_returns a Profile ID) ;
    $user_exists = HTTP request to couch DB view to return 
in JSON format the list of JSON documents with pid = $pid2
?>

Upvotes: 2

Views: 2160

Answers (1)

Tillmann Seidel
Tillmann Seidel

Reputation: 1042

Let your view emit the documents with doc.pid as the key

function(doc) { 
    emit(doc.pid, doc);
}

and use the key parameter to retrieve the right document:

http://localhost:5984/<database>/_design/<designdoc>/_view/<viewname>?key=<pid2>

This should return all documents with doc.pid === pid2.

Upvotes: 11

Related Questions