问题
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
回答1:
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.
回答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.
回答3:
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)
来源:https://stackoverflow.com/questions/61484148/firestore-how-search-in-the-entire-database