簡體   English   中英

如何使用 Ancestor 查詢和最新的 golang 庫從 Datastore 中讀取數據

[英]How to read from Datastore using an Ancestor query and latest golang libraries

我想從數據存儲區讀取所有實體(大約 6 個實體/記錄)。

我有一個數據存儲,它以我試圖理解的奇怪類型為鍵。 我在執行查詢的鍵上找不到任何唯一性。 The table looks like this: GCP Datastore 表示我想讀入 Go 應用程序的數據

When I click on a record, it looks like this:從這里開始暴露並使用鍵字面量來嘗試在 Go 應用程序中獲取記錄

``我可以像這樣在控制台中執行祖先查詢:```使用祖先查詢查詢的 GCP 數據存儲

偉大的! 所以現在我想從我的 Golang 應用程序中檢索這些數據? 但是如何? 我在網上看到很多關於使用q.Get(...) // where q is a *Query struct的解決方案這些解決方案中的任何一個都不起作用,因為它們導入google.golang.org/appengine/datastore 我知道這是遺留問題並且已棄用。 所以我想要一個導入cloud.google.com/go/datastore的解決方案。

我嘗試了這些方法,但運氣不佳:首先嘗試使用 GetAll 並查詢

我接下來嘗試了這個:第二次嘗試嘗試使用祖先查詢......還沒有准備好

最后我嘗試直接獲取單個記錄:最后我嘗試直接獲取記錄

在所有情況下,我的err都不是 nil 並且應該從數據存儲查詢填充的dts也是 nil。

任何幫助我了解如何查詢此密鑰類型的指導? 我是否遺漏了此表的鍵控和查詢方式的一些基本內容?

謝謝

然后我嘗試了這個:

看來您只是缺少命名空間

// Merchant Struct
type MerchantDetails struct {
    MEID  string
    LinkTo *datastore.Key
    Title string
}

// Struct array to store in
var tokens []MerchantDetails

// Ancestor Key to filter by
parentKey := datastore.NameKey("A1_1113", "activate", nil)
parentKey.Namespace = "Devs1"

// The call using the new datastore UI. Basically query.Run(), but datastore.GetAll()
keys, err := helpers.DatastoreClient.GetAll(
    helpers.Ctx,
    datastore.NewQuery("A1_1112").Ancestor(parentKey).Namespace("Devs1"),
    &tokens,
)
if err != nil {
 return "", err
}

// Print all name/id from the found values
fmt.Printf("keys: %v", keys)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM