慕尼黑8549860
2022-07-11 10:27:26
我正在嘗試向 Firestore 添加一個嵌套結(jié)構(gòu),由于某種原因,添加的內(nèi)容都是非結(jié)構(gòu),看起來像:結(jié)構(gòu)看起來像這樣: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) }}上面的代碼僅適用于不在嵌套結(jié)構(gòu)中的元素。對此的任何幫助將不勝感激更新 1Status不是子集合而是對象,例如:{ "name": "Test", "status": { "title": "hello", "message": "hi" }}
2 回答

拉風(fēng)的咖菲貓
TA貢獻1995條經(jīng)驗 獲得超2個贊
Firestore 被優(yōu)化為哈希條目和檢索數(shù)據(jù)存儲。因此,最好從結(jié)構(gòu)中創(chuàng)建映射。結(jié)構(gòu)對 Go 數(shù)據(jù)建模很有用,但是當(dāng)需要提交到數(shù)據(jù)庫時,請將其轉(zhuǎn)換為地圖。我通常只使用Fatih 的結(jié)構(gòu)來映射轉(zhuǎn)換器
它可以很容易地在 Go 端推理您的數(shù)據(jù),并且仍然能夠?qū)⑵涮峤灰赃M行存儲。

森林海
TA貢獻2011條經(jīng)驗 獲得超2個贊
根據(jù)對評論的討論,將此作為社區(qū) Wiki 答案發(fā)布。
這種情況的解決方案似乎是在 type 字段中手動添加值Map
。實現(xiàn)該目標(biāo)的步驟如下:訪問 Firebase 控制臺 -> Firestore -> 創(chuàng)建文檔 -> 添加類型的字段Map
。按照這個順序,可以創(chuàng)建一個 type 的字段,該字段Map
具有添加數(shù)據(jù)所需的格式,如問題描述中所述。
有關(guān)此類型、其工作原理、排序選項等的更多信息,可以在此處的官方文檔中找到:支持的數(shù)據(jù)類型。
- 2 回答
- 0 關(guān)注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消