[英]From within a view controller in a container view, how do you access the view controller containing the container?
[英]how do you access the values in a couchbase view?
我有一個widget.json文件,該文件已加載到couchbase的文檔中:
{
"type": "widget",
"name": "clicker",
"description": "clicks!"
}
我也有一個用於存儲桶的沙發床設計文檔,couchbase.ddoc。 它以名稱“ views”注冊:
{
"_id": "_design/accesscheck",
"language": "javascript",
"views": {
"all_widgets": {
"map": "function(doc, meta) { if(doc.type == 'widget') { emit(meta.id, doc.name); } }"
}
}
}
還有一些golang代碼,使用couchbase golang API來獲取它:
opts := map[string]interface{}{
"stale": false
}
data, _ := bucket.View("views", "all_widgets", opts)
在這一點上,我還有幾個問題:
一些Get()示例傳遞的是struct類型,即
type User struct { Name string `json:"name"` Id string `json:"id"` } err = bucket.Get("1", &user) if err != nil { log.Fatalf("Failed to get data from the cluster (%s)\\n", err) } fmt.Printf("Got back a user with a name of (%s) and id (%s)\\n", user.Name, user.Id)
是否可以使用View()做類似的事情?
我沒有在golang客戶端API示例或文檔中找到這些問題。 我可能錯過了一些東西。 如果有人有鏈接,請告訴我。
謝謝你的幫助!
如果您使用的是github.com/couchbaselabs/go-couchbase
,則可以使用bucket.ViewCustom
來解決您的問題。 它接受一個視圖結果解析為的項目。
bucket.View
只是使用預定義的struct調用bucket.ViewCustom
並返回它。
執行后的視圖結果是一個json對象,如下所示;
{
"total_rows": 123,
"rows": [
{
"id": "id of document",
"key": {key you emitted},
"value": {value you emitted},
"doc": {the document}
},
...
]
}
眾所周知,我們可以手動將結構類型設置為bucket.ViewCustom
。
就您而言,您可以像這樣為“ all_widgets”編寫自定義視圖結構;
type AllWidgetsView struct {
Total int `json:"total_rows"`
Rows []struct {
ID string `json:"id"` // document id
Key string `json:"string"` // key you emitted, the 'meta.id'
Value string `json:"value"` // value you emitted, the 'doc.name'
} `json:"rows"`
}
並使用bucket.ViewCustom
檢索值。
var result AllWidgetsView
opts := map[string]interface{}{}
viewErr := bucket.ViewCustom("views", "all_widgets", opts, &result)
if viewErr != nil {
// handle error
}
如果這種代碼模式頻繁出現,則可以對其進行重構。
對於問題2,它與golang無關,但與Couchbase本身有關。
視圖具有存儲桶所沒有的某些功能。 查看結果按鍵排序,因此您可以在開始的位置指定startkey
選項。 您可以使用此屬性並使視圖成為搜索對象。 有關更多信息,請參見查詢視圖 。
但是您需要更詳細的搜索(例如全文搜索),您應該使用ElasticSearch插件或N1QL
進行搜索。 (請注意, N1QL
是預覽版,尚未正式發布)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.