2017-09-09 43 views
0

我使用Cloudant數據庫,我想找回分貝爲匹配特定字段內的所有文件。 我的意思是 - 我只想得到那些具有特定值的字段的文檔。cloudant - 讓所有符合特定條件該文檔/場

你能幫我一個我可以測試的代碼的例子嗎? 在此先感謝

回答

1

一個很好但很普遍的問題。

您可以通過多種方式實現此目的。最經典的CouchDB的方法是創建鍵在球場上,你希望能夠來查詢,例如地圖,減少視圖(二級索引):

function (doc) { 
    if (doc && doc.surname) { 
     emit(doc.surname, 1); 
    } 
} 

您可以使用Cloudant儀表板創建這樣的觀點:Creating a view in the Cloudant dashboard

您現在可以查詢這個與特定姓氏的所有文件:

curl 'https://ACCOUNT.cloudant.com/examples/_design/example/_view/by_surname?limit=100&reduce=false&include_docs=true&startkey="kruger"&endkey="kruger0"' 

如果您希望能夠對字段的組合查詢,您可以創建一個量值鍵:

function (doc) { 
    if (doc && doc.surname && doc.firstname) { 
     emit([doc.surname, doc.firstname], 1); 
    } 
} 

被質疑,因爲這:

curl 'https://ACCOUNT.cloudant.com/examples/_design/example/_view/by_name?limit=100&reduce=false&include_docs=true&startkey=\["kruger", "stefan"\]&endkey=\["kruger","stefan0"\]' 

如果你是新來Cloudant,另一種方式來查詢是通過恰當地命名Cloudant查詢(亦稱芒果),這是一個基於json的聲明性查詢語言。

這是有據可查(https://console.bluemix.net/docs/services/Cloudant/api/cloudant_query.html),但它的要點是該類型的查詢:

{ 
"selector": { 
    "year": { 
     "$gt": 2010 
    } 
}, 
"fields": ["_id", "_rev", "year", "title"], 
"sort": [{"year": "asc"}], 
"limit": 10, 
"skip": 0 
} 
相關問題