【问题标题】:deleting a single dictionary from Fire-base real time database (python3)从 Fire-base 实时数据库中删除单个字典(python3)
【发布时间】:2019-05-17 04:38:50
【问题描述】:

fire-base数据库中的数据如下:

我的意图是一次与单个字典进行交互,例如从 fire-base 中删除它或更新它。为此,我必须使用多个过滤器过滤数据库,例如 home_teamaway_team

编辑

我已经成功地将数据过滤到我想要删除的特定字典中。附言。向 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' 的游戏

【问题讨论】:

  • @AndréKool 我看了看,结果我尝试了 .orderByChild('home_team').equalTo('Singapore Slingers') 没有成功。如果您知道使用 .orderByChild() 的更好方法,我无法做到这一点,那就太好了
  • 已取得进展并将其添加为问题的编辑

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


【解决方案1】:

嗯,

自己找到了答案,

首先我安装了 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

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2014-12-06
    • 2021-12-29
    • 2020-07-24
    • 1970-01-01
    相关资源
    最近更新 更多