doushang8846 2018-10-03 12:23
浏览 24
已采纳

如何返回文档索引名称值

This is a GoLang, Firebase AdminSDK question.

This example works to iterate through all of the documents in a FireStore DB.

How can I get the Document Name?

To put another way: If the collection name is JohnyCollection, and JohnyCollection has 20 Documents called (Document1, Document2.... Document20), how do I get the document name in golang Code?

//========================================

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

    }
}
  • 写回答

1条回答 默认 最新

  • drgc9632 2018-10-03 13:35
    关注

    You can get the document ID from the DocumentSnapshot, by first looking up the DocumentRef:

     fmt.Println(doc.Ref.ID)
    

    See the reference docs for DocumentSnapshot and DocumentRef.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角