簡體   English   中英

我如何使用 golang 中的特定鍵更新數據 firebase?

[英]How i can update data firebase with specific key in golang?

我正在使用 golang 和firego連接到 Firebase。我想使用密鑰IDAgent: 7將我的數據Status從 ON 更新為 OFF。 這是我的數據庫結構

圖片

假設:我不知道孩子 active_chat。 我如何更新 active_chat/-Koja8GuFplEN3kjbfPO 中的數據,其中 IDAgent = 7

我試過這段代碼

x := map[string]string{"Status": "OFF"}
ref.OrderBy("IDAgent").EqualTo("7").Update(x)

但是這段代碼查詢錯誤。

2022 年更新:

package main

import (
    "context"
    "fmt"
    "time"

    firestore "cloud.google.com/go/firestore"
    firebase "firebase.google.com/go"

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

type (
    myDocument struct {
        Cars       []Car  `firestore:"cars"`
        carsCount  int64  `firestore:"car_count"`
        UpdateTime string `firestore:"update_time"`
    }
    Car struct {
        Name      string `firestore:"name"`
        YearBuilt string `firestore:"year_built"`
    }
)

func getFirebaseClient(ctx context.Context) (*firestore.Client, error) {
    sa := option.WithCredentialsFile("Path_To_Firebase_Key")

    // Initialize firebase app with admin privileges
    app, err := firebase.NewApp(ctx, nil, sa)

    if err != nil {
        err = fmt.Errorf("getFirestoreClient failed: %s", err)
        return nil, err
    }
    // Create client
    client, err := app.Firestore(ctx)
    if err != nil {
        err = fmt.Errorf("failed to connect to firestore: %v", err)
        return nil, err
    }

    return client, nil
}

func main() {
    // Create context
    ctx := context.Background()

    // Get firebase client
    client, err := getFirebaseClient(ctx)
    if err != nil {
        panic(err)
    }

    // Create car struct
    newCar := Car{
        "Volvo_Series1",
        "1920",
    }

    // Update time
    newTime := time.Now().UTC().Format("Monday, 01-02-2006 15:04:05")

    // Updates to document
    updates := []firestore.Update{
        {Path: "cars", Value: firestore.ArrayUnion(newCar)},
        {Path: "car_count", Value: firestore.Increment(1)},
        {Path: "update_date", Value: newTime},
    }

    // OPTION A)
    // Create collection reference
    collectionRef := client.Collection("cars")

    // Create document reference
    docRef := collectionRef.Doc("12345")

    // Update document
    _, err = docRef.Update(ctx, updates)
    if err != nil {
        err := fmt.Errorf("failed updating document: %s from %s collection %v", docRef.ID, docRef.Parent.ID, err)
        panic(err)
    }

    // OPTION B)
    _, err = client.Collection("cars").Doc("12345").Update(ctx, updates)
    if err != nil {
        err := fmt.Errorf("failed updating document: %s from %s collection %v", docRef.ID, docRef.Parent.ID, err)
        panic(err)
    }
}

您可以通過兩種方式進行操作,如帶firego客戶端庫的Firebase文檔所示。 根據firego README.md起草的答案。

注意:您尚未提供結構的完整路徑,我已根據屏幕快照起草了答案。 因此,請相應地更新您的JSON路徑。

方法1:

f := firego.New("https://my-firebase-app.firebaseIO.com/active-chat/Koja8GuFpIEN3kjbfPO.json", nil)

x := map[string]string{
   "Status": "OFF",
}
if err := f.Update(x); err != nil {
  log.Fatal(err)
}

方法二:

f := firego.New("https://my-firebase-app.firebaseIO.com", nil)
f = f.Ref("/active-chat/Koja8GuFpIEN3kjbfPO.json")

x := map[string]string{
   "Status": "OFF",
}
if err := f.Update(x); err != nil {
  log.Fatal(err)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM