0

Data in fire-base database is as below:

enter image description here

my intention is to interact with a single dictionary at a time, such as delete it from fire-base or update it. To do this i have to filter through the database with multiple filters such as by home_team and away_team

edit

I have been successful in filtering through the data to the specific dictionary i would like to remove. ps. add rules to firebase as followed here: adding rules to firebase

current code is as below:

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

The expected result would be to delete the game whose home_team == 'Singapore Slingers' && away_team == 'CLS Knights Surabaya' from firebase real time database

Muhika Thomas
  • 128
  • 1
  • 7
  • Possible duplicate of [Query based on multiple where clauses in Firebase](https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase) – André Kool May 16 '19 at 17:41
  • @AndréKool i had a look at that and as a result i tried .orderByChild('home_team').equalTo('Singapore Slingers') to no success. If you would know a better way to work with .orderByChild() that i cant that'd be great – Muhika Thomas May 16 '19 at 17:55
  • Have made progress and added them as edits to the question – Muhika Thomas May 16 '19 at 20:00

1 Answers1

0

Well,

found the answer myself,

First i installed pyrebase4 pip install pyrebase4, find out more on this at pyrebase4 @ GitHub.

Next i edited rules @ Fire-base as:

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

my working code then sits as:

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
Muhika Thomas
  • 128
  • 1
  • 7