【问题标题】:Golang Firebase admin SDKGolang Firebase 管理员 SDK
【发布时间】:2019-03-14 17:31:30
【问题描述】:

假设 Firebase 数据存储 Db 是...

Collection("Collection1").
Document("documentX").
Collection("CollectionA").
field("FieldB")

在 Golang 中,使用 firebase admin SDK...在 GoLang 中获取 FieldB 值的代码是什么? 这是我尝试过的......

// dsnapp, err :=     client.Collection("Collection1/documentX").Get(ctx)
// dsnapp, err := client.Collection("Collection1").Document("documentX").Collection["CollectionA"].Get(ctx)

dsnapp2, err := client.Collection("Collection1").Doc("documentX").Get(ctx)
m = dsnapp2.Data()
fmt.Printf("222  Document data: %#v\n", m["CollectionA"]["FieldB")

还有其他各种技术……有什么建议吗?

【问题讨论】:

  • 字段不能直接存在于集合中。集合或子集合只能包含文档,文档包含字段。
  • 听起来像是道格的回答。
  • 如何检索整个文档,解组它然后从结构中获取值?

标签: firebase go google-cloud-firestore google-admin-sdk


【解决方案1】:

首先就像@Doug Stevenson 说的集合只包含文档。所以知道我们可以使用这个结构作为我们请求的基础。

Collection (CollectionA) -> Document (documentX) -> Collection (CollectionB) -> Document (documentY) -> Field (FieldB)

您应该考虑阅读一下 firebase admin sdk 的 go 文档。 https://godoc.org/cloud.google.com/go/firestore

您可以看到我们可以根据需要链接我们的请求。所以您的查询将如下所示:

dsnapp2, err := client.Collection("Collection1").Doc("documentX").Collection("Collection1").Document("documentY").Get(ctx)
m := dsnapp2.Data() // it is important to use the := operator if you havn't initialized the variable yet

fmt.Printf("222  Document data: %#v\n", m["FieldB")

但是,如果您需要多个字段,则应考虑将结果(例如第一个文档 (documentX))解组为结构。从那里您还可以获取所需的字段。

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 2019-12-28
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多