GoLang firebase管理员SDK,如何选择代码中的字段

时间:2018-10-10 21:47:29

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

随附的是Firebase dataStore门户的快照,后跟代码。该代码起作用并返回文档的映射。

问题:我如何在文档中获取特定字段?例如大小?而不是返回Document的地图,

换一种说法...我怎么能退回这个... [Collection01] [Document01] [SPECS] [Size] =字符串

或者这个... [Collection01 / Document01 / SPECS / Size] =字符串

Firebase门户

enter image description here

GOLANG代码

package main

import (
"fmt"
"log"

    "golang.org/x/net/context"

    firebase "firebase.google.com/go"

    "google.golang.org/api/iterator"
    "google.golang.org/api/option"
)

func main() {

ctx := context.Background()
sa := option.WithCredentialsFile("./scaiqit55-fb.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("Collection01").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...
    //  nacsid := doc.Ref.ID
    docdata := doc.Data()
    fmt.Println("------NEW INDEX----------")
    fmt.Println(doc.Ref.ID)
    fmt.Println(docdata)

    // Returns...
    //  ------NEW INDEX----------
    // Document01
    // map[SPECS:map[Color:red SIZE:Large]]


    //---------------------------
    //  My question is here...
    // How would i return...
    // [Collection01][Document01][SPECS][Size]
    //  and just get a string of "large"

    // this is where the answer would go...

    }
}

0 个答案:

没有答案