2017-07-17 91 views
0

我正在使用Angular,PHP和Cloudant構建應用程序。通過屬性查詢Cloudant,而不是通過Angular和PHP獲取所有文檔

現在我剛剛查詢所有文檔,然後使用Angular處理/過濾/顯示數據,但是我感覺這會在文檔數量增加時接近糟糕的練習。

考慮到當前的代碼,我如何通過文檔屬性進行查詢而不是獲取所有文檔?

角碼:

var $promisedbex=$http.get('databaseconnect/getalldiscovery.php'); 

$promisedbex.then(function (data) { 
    $scope.dataex = []; 
    $scope.dataex = data.data.rows; 
    console.log($scope.dataex); 
}); 

PHP代碼:

<?php 
    $url = "https://user:[email protected]/discovery/_all_docs?include_docs=true"; 
    $ch = curl_init(); // initialize curl handle 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    $output = curl_exec($ch); 

    $info = curl_getinfo($ch); 
    curl_close($ch); 
?> 

文件例如:

{ 
    "_id": "1339ede5175888cb31e7f96aabd296bf", 
    "_rev": "1-7dc3fd24ce2a3846fcbae0e47daab829", 
    "fair": "Koelner Liste", 
    "fairyear": "2017" 
} 

我怎麼能由fair例如查詢?

+1

您必須創建視圖來發出要查詢的屬性。看看谷歌如何處理意見。如果您使用的是CouchDB 2.0,您可以創建索引並使用Mango Query Selectors(這是一種類似於MongoDB的查詢語言,它比視圖更爲靈活)。 –

回答

0

嘗試創建一個使用fair作爲其關鍵字的視圖。這將使您能夠使用此密鑰作爲基礎來限制和/或排序結果。 map函數可能看起來像:

function (doc) { 
    emit(doc.fair, null); 
} 

時,取視圖中的數據,仍然使用include_docs參數,也可以考慮使用startkeyendkey成過濾neeeded。

相關問題