如何 return 文档索引名称值
How to return Document Index Name Value
这是一个 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
and DocumentRef
的参考文档。
这是一个 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
and DocumentRef
的参考文档。