亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將嵌套結構添加到 Firestore

將嵌套結構添加到 Firestore

Go
慕尼黑8549860 2022-07-11 10:27:26
我正在嘗試向 Firestore 添加一個嵌套結構,由于某種原因,添加的內容都是非結構,看起來像:結構看起來像這樣:type Status struct {    Title   string `json:"title,omitempty" firestore:"title,omitempty"`    Message string `json:"message,omitempty" firestore:"title,omitempty"`}type Config struct {    Name   string `json:"name,omitempty" firestore:"name,omitempty"`    Status Status `json:"status,omitempty" firestore:"status,omitempty"`}代碼看起來像這樣:import (    "context"    firebase "firebase.google.com/go/v4"    "google.golang.org/api/option")func main() {    configuration := Config{        Name: "Test",        Status: Status{            Title:   "hello",            Message: "hi",        },    }    ctx := context.Background()    config := firebase.Config{        ProjectID:     "",        StorageBucket: "",    }    opt := option.WithCredentialsFile("firebase_config.json")    app, err := firebase.NewApp(ctx, &config, opt)    if err != nil {        panic(err)    }    // Get an auth client from the firebase.App    client, err := app.Firestore(ctx)    if err != nil {        panic(err)    }    _, _, err = client.Collection("forecast").Add(ctx, configuration)    if err != nil {        panic(err)    }}上面的代碼僅適用于不在嵌套結構中的元素。對此的任何幫助將不勝感激更新 1Status不是子集合而是對象,例如:{  "name": "Test",  "status": {      "title": "hello",      "message": "hi"   }}
查看完整描述

2 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

Firestore 被優化為哈希條目和檢索數據存儲。因此,最好從結構中創建映射。結構對 Go 數據建模很有用,但是當需要提交到數據庫時,請將其轉換為地圖。我通常只使用Fatih 的結構來映射轉換器

它可以很容易地在 Go 端推理您的數據,并且仍然能夠將其提交以進行存儲。


查看完整回答
反對 回復 2022-07-11
?
森林海

TA貢獻2011條經驗 獲得超2個贊

根據對評論的討論,將此作為社區 Wiki 答案發布。

這種情況的解決方案似乎是在 type 字段中手動添加值Map。實現該目標的步驟如下:訪問 Firebase 控制臺 -> Firestore -> 創建文檔 -> 添加類型的字段Map。按照這個順序,可以創建一個 type 的字段,該字段Map具有添加數據所需的格式,如問題描述中所述。

有關此類型、其工作原理、排序選項等的更多信息,可以在此處的官方文檔中找到:支持的數據類型。


查看完整回答
反對 回復 2022-07-11
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號