从Fire-base实时数据库(python3)删除单个词典

时间:2019-05-16 17:27:40

标签: python-3.x firebase firebase-realtime-database filter

火基数据库中的数据如下:

enter image description here

我的目的是一次与一个字典进行交互,例如从火库中删除它或更新它。为此,我必须使用多个过滤器(例如 home_team away_team

)对数据库进行过滤

修改

我已经成功地将数据过滤到要删除的特定词典中。 ps。将规则添加到firebase中,如下所示:adding rules to firebase

当前代码如下:

import pyrebase

# firebase configuration
########################
config = {
    ...
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()

games = db.child("basketball").order_by_key().order_by_child("home_team").equal_to("Singapore Slingers").get()
print(games.val())  # success 

# now to delete 
db.child("basketball").order_by_key().order_by_child("home_team").equal_to("Singapore Slingers").remove()  # this clears the entire database even though i have filtered to the database i would like to remove

预期结果将是从firebase实时数据库中删除home_team =='Singapore Slingers'&& away_team =='CLS Knights Surabaya'的游戏

1 个答案:

答案 0 :(得分:0)

好吧

自己找到答案,

首先我安装了pyrebase4 pip install pyrebase4,请在pyrebase4 @ GitHub上找到更多内容。

接下来,我将规则@ Fire-base编辑为:

{
"rules": {
    "basketball" : {
          ".indexOn": ["home_team", "away_team"]
        },
    ".read": true,
    ".write": true
  }
}

然后我的工作代码为:

import pyrebase

# firebase configuration
########################

config = {
    ...
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()

# game filter
games = db.child("basketball").order_by_child("home_team").equal_to("Singapore Slingers").order_by_child("away_team").equal_to("CLS Knights Surabaya").get().val()

# Gēmu o korosu
for game in games:
    db.child("basketball").child(game).remove()
# works like a charm