Data for one of our application use case is stored in firestore in below format:
"Collection" : "ROOT_COLLECTION"
"Documents" : MAIN_ID_1 : attribute : is_enabled:1
"subcollection": "Sessions"
Documents : Session_1: attribute: is_merged:false
Session_2: attribute: is_merged:true
Session_3: attribute: is_merged:false
MAIN_ID_2 : attribute : is_enabled:2
"subcollection": "Sessions"
Documents : Session_4: attribute: is_merged:false
Session_5: attribute: is_merged:true
Session_6: attribute: is_merged:true
MAIN_ID_3 : attribute : is_enabled:1
"subcollection": "Sessions"
Documents : Session_7: attribute: is_merged:true
Session_8: attribute: is_merged:true
Session_9: attribute: is_merged:true
MAIN_ID_4 : attribute : is_enabled:1
"subcollection": "Sessions"
Documents : Session_10: attribute: is_merged:false
Session_11: attribute: is_merged:false
......
Here we want to find all last level sessions from root collection documents where is_enabled is true and sessions where is_merged is false in a single query, so that we can put listener on top of all retrieved sessions.
Query : is_enabled on documents in root collection : 1
is_merged on documents in session collection : false
Result should be session documents: Session1,Session3,Session7,Session8,Session9
How can achieve this using single chained/combined query from firestore and listener on it ?
I would not try to make this work with this structure. It would not be easy and there is a much better way of doing it.
You seem to have fallen into the trap of using Firestore like a relational database instead of a document store.
If you want to keep this structure then I would suggest adding a cloud function or something that would update the top level documents when a session in a subcollection is updated. This way you can put a value like contains_unmerged_session: Bool
or something in it. That feels hacky though.
A possible better way would be to have a top level collection of sessions
and then in each session store a reference to the parent in the session document. It's hard to tell from your question how that relationship works but that's probably what I would do.
The best approach when doing anything in Firestore is to structure your data how you want to get it on the client. If the structure makes it difficult to query... change the structure.
Queries can only execute against a single collection, or against collections of the same name. Queries can only consider data in the documents they return, they have no way to look at data in other documents.
Combined, these two limits means that your use-case cannot be implemented as a single query on your current data model.
Your options are to:
is_enabled
, and then loop over the results, and query their sessions
for the documents matching is_merged:false
.is_enabled
into each underlying sessions
document, and perform the query with a collection group query across all sessions
subcollections.Neither if these is pertinently better than the other. As usual when dealing with NoSQL databases, you're trading off increased data storage and a more complex data model for a faster and simpler query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.