Reputation: 2795
Hello! I'm trying to make two Firebase queries:
All children under basicInfo
, for multiple users at once (query limited by 20 results). For example, in this query with the above database I will get all basicInfo
sections for -Kwz1eoUMPym...
and also for -Kwzhj4pzBJxbb...
All children under basic info where name == (some name)
.
I can't understand how to approach a name
query under the AutoID key and also under basicInfo
key, for both queries.
What is the most logical approach to this?
Upvotes: 1
Views: 687
Reputation: 6926
The Firebase Realtime Database is a NoSQL database, and it's only possible to filter by direct descendents of children in a list. Therefore, you will need to use a flatter data structure, and avoid splitting your fields into basicInfo
, fullInfo
:
users
|- userId
|- name: "John"
|- age: 17
|- gender: "m"
|- birthday: "10.10.2000"
...
With this approach, you can attach a listener to the users
node and then filter by child values using the queryOrderedByChild
and queryEqualToValue
methods. For example, something like this in Swift:
Database.database().reference().child("users").queryOrderedByChild("name").queryEqualToValue("(some name)").observeSingleEventOfType(.Value, with: { snap in
// Do your thing with snap
})
With either of your structures, you can limit your results with the queryLimitedToFirst
and queryLimitedToLast
methods. For example, from the filtering data section of the documentation:
The following example demonstrates how example blogging app retrieves a list of the 100 most recent posts by all users:
// Last 100 posts, these are automatically the 100 most recent // due to sorting by push() keys let recentPostsQuery = (ref?.child("posts").queryLimited(toFirst: 100))!
For more details on filtering and limiting Firebase data in Swift/iOS, see the working with lists of data on iOS documentation.
Upvotes: 3