loliki
loliki

Reputation: 957

iOS Firebase get data where value equals to something

How can I get data in swift3 from firebase where the value equals something.

In MySQL that would be "SELECT * FROM users WHERE type = 'brown'"; how can I write that where part in Firebase?

I have this DB

enter image description here

And I want to get all the data from it where month == currentMonth (I have a currentMonth variable)

Upvotes: 3

Views: 1733

Answers (2)

Haroldo Gondim
Haroldo Gondim

Reputation: 7995

You can Query like this:

let ref = Database.database().reference()
ref.child("Budget")
    .queryOrdered(byChild: "Month")
    .queryEqual(toValue: "November")
    .observe(.value, with: { (snapshot: DataSnapshot) in

        var array:[Budget] = []

        for budgetChild in snapshot.children {
            let budgetSnapshot = budgetChild as! DataSnapshot
            let budgetDictionary = budgetSnapshot.value as! [String:String]

            let budget: Budget = Budget()
            budget.description_ = budgetDictionary["Description"]
            budget.month = budgetDictionary["Month"]
            budget.type = budgetDictionary["Type"]
            budget.value = budgetDictionary["Value"]

            array.append(budget)
        }
    })

Upvotes: 3

MorganIsBatman
MorganIsBatman

Reputation: 1010

As far as I'm aware firebase realtime database doesn't have support for querying like this. Instead you need to call down the whole ref 'budget' then filter it locally in your app.

FireStore apparently has better query support although its in Beta.

Upvotes: 0

Related Questions