2012-12-13 88 views
0

的第2和第4級按鍵語言過濾器....借給予CouchDB中下面的文檔CouchDB文檔

{ 
    "_id": "002bafd55b353692a7ab2968074310cc2cbff258", 
    "_rev": "1-bc853056ac61d817ae3c4ecb4f81322b", 
    "names": [ 
    { "locale": "en", "value": "Example" }, 
    { "locale": "de", "value": "Beispiel" }, 
    { "locale": "fr", "value": "Exemple" } 
    ], 
    "details": [ 
    { "locale": "en", "value": "An Example is here" }, 
    { "locale": "de", "value": "Ein Beispiel ist heir" } 
    { "locale": "en", "value": "Un exemple est ici" } 
    ] 
} 

...我怎麼能寫觀點,讓我回 部分文檔過濾了不需要的語言?

  • curl ..snip.. '_design/locale_filter/?locale=en,de,fr,it'
  • curl ..snip.. '_design/locale_filter/?locale=en,fr'
  • curl ..snip.. '_design/locale_filter/?locale=en'

應該返回的東西看起來像這樣:

{ 
    "_id": "002bafd55b353692a7ab2968074310cc2cbff258", 
    "_rev": "1-bc853056ac61d817ae3c4ecb4f81322b", 
    "names": [ 
    { "locale": "en", "value": "Example" }, 
    ], 
    "details": [ 
    { "locale": "en", "value": "An Example is here" }, 
    ] 
} 

還有一分的情況下,其中的文件有一個進一步的更深層次的架構, 其重複細節結構,這也將是 在一個理想的世界過濾:

{ 
    "_id": "002bafd55b353692a7ab2968074310cc2cbff258", 
    "_rev": "1-bc853056ac61d817ae3c4ecb4f81322b", 
    "names": [ ... snip ... ], 
    "details": [ ... snip ... ] 
    "deeper": { 
     "names": [ 
     { "locale": "en", "value": "Sub-Example" }, 
     ], 
     "details": [ 
     { "locale": "en", "value": "The Sub-Example is here" }, 
    } 
} 

我還要注意的是,這可能不是一個視圖,而是顯示,從 文檔的CouchDB說,表演是一種用於將文件轉換成任何 格式。

從初學者的最終查詢是否是有一些方法,使其更容易 對CouchDB的意見和設計文檔的工作,現在我有 erica感覺就像矯枉過正試驗因爲我 肯定我不想要沙發應用程序,我只想在磁盤上的文件中輕鬆維護我的視圖 ,並且只要我做出了足夠大的更改即可將其與沙發數據庫同步。

回答

0

我能實現這個使用show function,我實現了兩個節目的功能,一個用於方便:

(doc, req) -> 
    all_locales = [] 
    for name in doc.names 
    all_locales.push name.locale 
    toJSON(all_locales) 

(我也實現了它在details,在我真正的代碼刪除重複的語言環境)

這使我能夠做到以下幾點:

GET /_design/dbname/_show/list_locales/c0db9ad..snip.. 

,並返回["en", "de", "fr"],例如 - 無論LOCA語言碰巧有。

那麼我可以跟進的函數來檢索過濾文件:

(doc, req) -> 
    locales = req.query.locales.split(",") 
    doc.names = doc.names.filter (name) -> 
    locales.indexOf(name.locale) > -1 
    doc.overviews = doc.details.filter (overview) -> 
    locales.indexOf(overview.locale) > -1 
    return toJSON(doc) + "\n" 

這種情況的使用模式是:

GET /_design/dbname/_show/restrict_locales/c0db9ad..snip..?locales=en,fr 
GET /_design/dbname/_show/restrict_locales/c0db9ad..snip..?locales=fr 
GET /_design/dbname/_show/restrict_locales/c0db9ad..snip..?locales=en,fr,de,it,hu,zh 

它工作得非常好,而且比快得多我期望。我相信CouchDB會大力緩存show function的結果。