1

I hope you are well !!

My database looks like below:

  • / CLASSROOM1 [collection] ----> DATAS [document] ----> USERS [collection] ----> userdId [document] ----> [user data (field)]

  • / CLASSROOM2 [collection] ----> DATAS [document] ----> USERS [collection] ----> userdId [document] ----> [user data (field)]

  • / CLASSROOM3 [collection] ----> DATAS [document] ----> USERS [collection] ----> userdId [document] ----> [user data (field)]

I would like to know if it is possible to search the entire Firestore database (CLASSROOM1, CLASSROOM2, CLASSROOM3 ...) and find the correspondence with the "matricule" entered by the user.

The goal here is to find the field "matricule" found in the document "userId" and to read its value, so as to know which user it belongs to.

Thank you. I start on Firestore

Steeven Delucis
  • 293
  • 2
  • 14

3 Answers3

2

It's not possible to perform a single query across multiple collections with different names. Each collection will require its own query, and you will have to merge the results from those queries on the client.

The only type of query that can use multiple collections is a collection group query, which requires that each subcollection must have the same name.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
2

I don't think this would be efficient. As @Doug mentioned above, each collection will require it's own query which would ofcourse empty your pocket.

Best way to do this would be create a different collection which contains the matricule as documents and store the {user id} in that document per user. Then when you want to fetch the data, query this new collection to get the {user id}. You know what the user ID is, now you fetch anything regarding that user in your Firestore Database.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • I understand, I will try the idea that you gave me – Steeven Delucis Apr 28 '20 at 16:06
  • @Delucis cook, let us know the results. – Dharmaraj Apr 28 '20 at 16:11
  • sorry for the late response. Well, here is what I did, based on the idea you told me about, I created a collection "MATRICULES" which contains documents whose names are matricule names, and each these documents contain a child userId which represents the id of a user. – Steeven Delucis Apr 29 '20 at 14:51
  • @Delucis so any useful outcome? – Dharmaraj Apr 29 '20 at 14:53
  • @Delucis if you got your solution make sure you accept the answer. This will help others know that the question is solved :-) – Dharmaraj Apr 29 '20 at 15:03
  • Here is what is going on (the logic behind all this), the user from the application enters his **matricule**, I check if the **matricule** entered corresponds to a document name found in the "MATRICLES" collection if this is the case , we access this document and retrieve the userId field and from its retrieved id, I retrieve its information in USERS [collection] ----> {userId} [document]. Did i do well? Any suggestion? – Steeven Delucis Apr 29 '20 at 15:03
  • 1
    @Delucis yes correct. We are first looking for the user id of that particular matricule, checking if it exists and you got it. – Dharmaraj Apr 29 '20 at 15:08
  • Thanks for your help @Dharmaraj – Steeven Delucis Apr 29 '20 at 15:15
0

I assume you have userId as input and wnat the corresponding matricule. Add "userId" as a field, then you can do a collection group query on the collections "USERS"

collectionGroup("USERS").where("userId", "==", userId)
l1b3rty
  • 3,333
  • 14
  • 32