Vicki Mes
Vicki Mes

Reputation: 35

FireStore Query sub collection

i have the following structure in FireStore,

I want to have questions in the questions collection, then, when a user answers a question, his/her result is stored in the question's sub collection with user's string id as the id. My problem is, how can i query to get questions a user has not answered. i.e unanswered questions are those that don't have a result with user's id as their id. Hope am clear enough on this. Thanks.FireStore Screenshot

val questionsRef = db.collection("questions")
val query = questionsRef.whereEqualTo(
        questionsRef.document().collection( "results" ).document(App.INSTANCE.appUUIDString), null)
        .limit(50).get()

Upvotes: 0

Views: 1113

Answers (1)

joelm
joelm

Reputation: 8771

One approach would be to have an object in the User's record (or a separate collection indexed by userId) that lists which questions they have answered and then query those that they have not answered. (This would require getting the list of all questions and removing those that are answered.) You'll have to retrieve the questions one-by-one in a loop.

In a no-sql database you will end up with multiple indexes for this type of structure.

Upvotes: 1

Related Questions