1

I need a firebase query to filter the list based on the value of an array.

enter image description here

if any of the index of GID(Array) contains the given key. e.g my key is YsGMyfLSGRNHxDWQmhpuPRqtxlq1 and one node's GID have that on 0th index and other have that on 1st index. So these two lists need to be returned.

Currently, I can only get the one at 0th index using the code

//userID = YsGMyfLSGRNHxDWQmhpuPRqtxlq1
 firebaseDatabase.child("Groups").queryOrdered(byChild: "GID/0").queryEqual(toValue:userID)

When I try to combine the query I am getting errors.

Bhavik Modi
  • 1,517
  • 14
  • 29
RöHIT
  • 29
  • 7
  • There is a limitation in Firebase Realtime database that you can only orderby 1 child. But please share your db structure, I'll try to give you solution. – Asad Ali Choudhry May 30 '19 at 09:45
  • Please try this firebaseDatabase.child("Groups").queryOrdered(byChild: "GID").queryEqual(toValue:userID) – Asad Ali Choudhry May 30 '19 at 09:59
  • Your current database structure makes it easy to find the GIDs (users?) for a group. It does however not make it easy to find the groups for a GID. To allow the latter, you'll want to add a reverse index: `/GIDS/$gidid/groups/$groupId` to your data structure. Also see my answers here: https://stackoverflow.com/questions/27207059/firebase-query-double-nested and here: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen May 30 '19 at 13:44

2 Answers2

0

I don't know about your database structure, But I can explain that There is a limitation in Firebase Realtime database that you can only order by 1 child. So now if we require to order by 2 Childs we can combine 2 nodes and make it 1 node and can apply order by query on it. For example

If we have username & email fields we can make a new field username_email and can apply order by on it.

Like

user: {
     username: "john",
     email: "john@g.com"
     username_email = "john_john@g.com"
}

Now we can write

firebaseDatabase.child("user").queryOrdered(byChild: "username_email").queryEqual(toValue: "john_john@g.com");
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
  • In my case in child "username_email" , I only know value before _ like "john". Is there a way that we can query that if "user_email" contains value john? like seprating that with _. – RöHIT May 31 '19 at 03:44
  • No @RöHIT in Firebase database SDK we cannot query contains, its limitation of realtime database SDK. Now what you can do either use firebase API or write a cloud function and fetch data by calling your cloud function after performing your all checks in your function. – Asad Ali Choudhry May 31 '19 at 07:36
0

There is no way you can filter your groups based on a value that exist within an array. If you want to query your database to get all groups a particular user is apart of, then you should consider augmenting your data structure to allow a reverse lookup. This means that you should add under each user object the groups in which that user is present.

This means that you'll need to duplicate some data, but this is not a problem when it comes to Firebase. This is a quite common practice, which is named denormalization and for that, I recommend you see this video, Denormalization is normal with the Firebase Database.

When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.

However, what you need is actually allowed in Cloud Firestore. Its array-contains operator allow you to filter documents that have a certain value in an array. For more on this topic, please see the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193