firebase - 如何返回文档索引名称值
问题描述
这是一个 GoLang、Firebase AdminSDK 问题。
此示例用于遍历 FireStore 数据库中的所有文档。
如何获取文档名称?
换句话说:如果集合名称是JohnyCollection
,并且JohnyCollection
有 20 个名为 ( Document1
, Document2
.... Document20
) 的文档,我如何在 golang 代码中获取文档名称?
//==========================================
package main
import (
"context"
"fmt"
"log"
"firebase.google.com/go"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
ctx := context.Background()
sa := option.WithCredentialsFile("./scai-qit-fb-adminsdk.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Close()
iter := client.Collection("COMPLEX_NONACS").Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
//This part works. WIll return a Map of each Document
fmt.Println("--------------------------/n")
fmt.Println(doc.Data())
// This is the question. How do I get the INDEX name of the Document?
// something like...
fmt.Println(doc.Index_value_or_something_that_returns_IndexName())
// for example...
// {
// "ABC":{"line1":"yabba dabba","line2":"dingo dong"},
// "DEF":{"line1":"hooty tooty","line2":"blah blah"}
// }
// How to just get the "ABC" and "DEF"
}
}
解决方案
DocumentSnapshot
您可以通过首先查找 ,来获取文档 ID DocumentRef
:
fmt.Println(doc.Ref.ID)
请参阅 和 的参考DocumentSnapshot
文档DocumentRef
。
推荐阅读
- firebase - 无法使用 cypress 使用 Firestore 本地模拟器测试应用程序
- r - 如何在包中设置选项
- python - 如何在python中应用控制点网格来变形图像
- php - 如何在magento2中阻止礼品卡页面的货币切换器
- node.js - 用于自动完成的 VS 代码 npm 扩展对我不起作用
- c# - 使用 TestServer 进行 ASP.net 核心集成测试有什么优势?
- html - 修复了使用带有伪元素的 CSS 网格的纵横比网格布局在 Safari 或 Firefox 中不起作用
- c# - 将 VB.NET 委托转换为 C#
- c++ - 使用类的构造函数时出错
- c# - 我的 webrequest 不工作得到 400 错误消息?