登录
首页 >  Golang >  Go问答

管理Firebase的Golang SDK

来源:stackoverflow

时间:2024-03-02 16:09:24 240浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《管理Firebase的Golang SDK》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

假设 firebase 数据存储数据库是...

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")

以及其他各种技术...有什么建议吗?


解决方案


首先就像@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))解组到结构体。从那里您还可以获取所需的字段。

今天关于《管理Firebase的Golang SDK》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>