Query in Firestore obtaining documents with two array-contain

I am aware it is not a RS directly related question, but I want to find a solution or approach integrated into it and I know there are several users working in projects with Firestore so maybe someone could help me figure this out.

I am trying to find an approach to get something that is not possible as it is. Firestore does not allow two “array-contain” in a query, but there must be a way to get what I am looking for. Maybe changing the way I am saving the data.

My project is linked to a database in Firestore where there are two collections:

    students (collection)
      - student1ID (document)
         + name (string)
         + more data (string)
         + teachers (array of teacher1ID, teacher2ID...)
         + subjects (array of Maths and English)
      - student2ID (document)
         + name (string)
         + more data (string)
         + teachers (array of teacher1ID, teacher2ID...)
         + subjects (array of Maths and Spanish)
   teachers (collection)
      - teacher1ID (document)
         + name (string)
         + more data (string)
         + students (array of student1ID, student3ID...)

It is quite simplified but just to get the idea. The user of the project is the teacher. In order to set grades for his students -llisted in the array in his own document- I have created a query for all students where the array contains the subject selected, and then some = clauses but still, the result of the query may give students that are not from that teacher because there will probably be more math teachers.

An ideal query would be to get documents (‘subjects’,‘array-contains’,‘math’) && (‘teachers’,‘array-contains’,‘student1ID’). That way, student 2 wouldn’t be listed even though he/she studies Math.

I hope I explained it properly. Thanks!

What if you add an extra field which contains combination of teacher id + subject? Then you could create a query which returns only the students who attend to certain teacher’s subject?

1 Like

That could be it. You suggestion could solve some other inconvenients that I have come across with some queries. However let me ask because I believe there are several ways to do it:

  • I believe you suggestion is to add in the array “teachers” not just the “teacherID”, but the “teacherID+subject” in one single field. I can do that creating some basic scripts. Truth is that I don’t see a problem with this option, but I would have to implement it and test to see if there is something I am not thinking about.

  • But maybe you were talking about adding a map in Firestore, which is basically what I believe is called an object in JS (correct me if I make a mistake with the terminology). In a map I can specify the “teacher: subject”. This approach seems to be also good. I thought about this previously but I am not sure how to query these maps and instead of stop for a minute and read the documentation, I moved ahead quickly doing what I know how to do with the arrays.

So I think I could do it both ways, which one do you recommend?

Thanks!

I was thinking to keep it simple and just add a new field with value “teacherID+subject”. This way you can easily do the queries by using Data sheet’s query field.

Ok! Thank you so much for your answer. I will work on that and let you know but I believe I can get it done.

Maybe I should create a new topic but I will quickly ask here. When I compile or open my exported project I get a lot of warnings similar to “expected = and got ==”. I understand this is not critical and I would know how to manually fix it in the exported project, but I would get again these warnings in a new export from RS. To fix this within RS that should be done into the code (Code glance), but that section is not editable.

Is there a way to fix these warnings within RS so they are not there after fixing them in the future exports?

Unfortunately you cannot edit the code so that it would not be overwritten in the export. The warnings in the code are something that we need to fix in the code exporter at some point.

Ok! No problem at all. I was just wondering if I was missing something to fix that. I understand you have your ongoing paid projects and can only dedicate some time to RS when you it is possible. I really appreciate not just your great work with RS but the time and answers here. I am learning quite a lot about JS and React since I started with my first project with RS.

Thanks!

Just a quick update to this. I could implement the idea and it is working perfectly. Thank you so much!

1 Like