2014-12-30 61 views
1

我正在從我的Web應用程序(我的第一個應用程序)從SQL移動到Couch DB。Couch DB - 傳遞輸入參數以查看

雖然我不能說我爲什麼不喜歡SQL查詢,但不確定我不知道,但是使CURL請求訪問我的數據庫聲音的想法必須比使用PHP PDO更好。

我花了一天半的時間試圖熟悉沙發DB HTTP API。我不能聲稱我已經完全閱讀了API,但是在開始編寫代碼之前,誰會徹底讀取API。所以我可能很愚蠢的問題是 - how do I pass an variable other than doc to a map function while making a http request to the view. API清楚地表明,map函數只接受一個「doc」參數,在這種情況下,它下面的函數是錯誤的,但我在API中找不到任何部分讓我使用最終用戶提供的輸入來查詢數據庫。

我的映射函數是

function(doc, pid2){ 

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

    } 
} 

PID2是將由前端用戶提供一個數字。

<?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 
?> 

回答

7

讓您的觀點發出的文件與doc.pid爲重點

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

,並使用key參數來檢索正確的文件:

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

這應返回所有文件與doc.pid === pid2