I need to get only the communities where in the cities list they have, match with a specific locationID
Unfortunately, you cannot achieve this using your actual database structure.
That works perfectly when the child location is not an "array".
It works when the location is a property of type String and not an array because the type of the object that you can pass to the equalsTo()
method was of type String and not array.
So there is any way to do it?
To solve this, you should duplicate your data. This practice is called denormalization
and is a common practice when it comes to Firebase. If you are new to NoSQL databases, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding.
Also, when you are duplicating data, there is one thing that needs to keep in mind. In the same way, you are adding data, you need to maintain it. In other words, if you want to update/delete an item, you need to do it in every place that it exists.
That being said, in your particular case, you should consider augmenting your data structure to allow a reverse lookup by creating another node named comunityCities
, where you should add as objects all corresponding communities. So your database structure should look similar to this:
Firebase-root
|
--- comunityCities
|
--- locationID
|
--- communityID
| |
| --- //Community details
|
--- communityID
|
--- //Community details
Using this schema, you can simply query the database to get all the comunities that corresponde to single location, by using the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference comunityCitiesRef = rootRef.child("comunityCities");
comunityCitiesRef.addListenerForSingleValueEvent(/* ... */);
You can read more information in my answer from the following post:
It's an answer for a Cloud Firestore question but the same rules apply in the case of the Firebase Realtime Database.
Or I just need to bring all the communities, and then filter in the device?
There's no need for that. Using this solution you'll only get the desired elements.