Reputation: 102
How i can make query in firebase? On oracle sql the query would be
"select * from users where username = "SomeUser"
I need to do the same in Firebase, Swift 3
Upvotes: 2
Views: 3209
Reputation: 1588
You need to use queryOrderedByChild
and queryStartingAtValue
. This should get you started in the right direction.
queryStartingAtValue(username).queryEndingAtValue(username)
let username = "SomeUser"
FIRDatabase.database().reference.child("users").queryOrderedByChild("username").queryStartingAtValue(username).queryEndingAtValue(username).observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
}
queryEqual(toValue: username)
let searchRef = FIRDatabase.database().reference().child("users").queryOrdered(byChild: "username").queryEqual(toValue: username)
Upvotes: 3
Reputation: 344
Swift 3 && Firebase 3
I hope this will help you understand. Here we go
This is example of the data structure:
And here is the code:
let username = "DavidMouse"
// create searchRef or queryRef you name it
let searchRef = FIRDatabase.database().reference().child("users").queryOrdered(byChild: "username").queryEqual(toValue: username)
// search the username
searchRef.observeSingleEvent(of: .value, with: { (snapshot) in
guard snapshot.value is NSNull else {
// yes we got the user
let user = snapshot.value as! String
print("\(user) is exists")
return
}
// no there is no user with desire username
print("\(username) is exists")
}) { (error) in
print("Failed to get snapshot", error.localizedDescription)
}
Upvotes: 3