[英]How i can update data firebase with specific key in golang?
I am using golang and firego for connecting to Firebase. I want to update my data Status
from ON to OFF with key IDAgent: 7
.我正在使用 golang 和firego连接到 Firebase。我想使用密钥IDAgent: 7
将我的数据Status
从 ON 更新为 OFF。 This is my Database Structure这是我的数据库结构
Assumption: I don't know child active_chat.假设:我不知道孩子 active_chat。 How can i update data in active_chat/-Koja8GuFplEN3kjbfPO where IDAgent = 7我如何更新 active_chat/-Koja8GuFplEN3kjbfPO 中的数据,其中 IDAgent = 7
I have tried this code我试过这段代码
x := map[string]string{"Status": "OFF"}
ref.OrderBy("IDAgent").EqualTo("7").Update(x)
but this code wrong query.但是这段代码查询错误。
Update for 2022: 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)
}
}
In two ways you can do, as per Firebase doc with firego client library. 您可以通过两种方式进行操作,如带firego客户端库的Firebase文档所示。 Drafted answer based on from firego README.md . 根据firego README.md起草的答案。
Note: You have not provided the complete path of the structure, I have drafted the answer based on screenshot. 注意:您尚未提供结构的完整路径,我已根据屏幕快照起草了答案。 So update your JSON path accordingly. 因此,请相应地更新您的JSON路径。
Approach 1: 方法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)
}
Approach 2: 方法二:
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.