0

I have a structure of nested subcollections and documents. Each name of the documents in the experience subcollection represents userId of some user that has experience in that game.

DB structure

I have a converted that attaches this userId to the document:

experienceEntity.userId = snapshot.id;
experienceEntity.experience = snapshot.data().experience;

I want to query documents from all the experience subcollections of all the games that have a certain userId. E.g. I want to get all documents having 400riCGTB0YkxAF52maPUr5jKPN2 ID from the screenshot above. I was trying this:

const result = await firestore()
  .collectionGroup("experience")
  .withConverter(gameExperienceEntityConverter)
  .where("userId", "==", userId)
  .get();

return result.docs.map((snap) => snap.data());

... but it returns an empty array. If I remove the where clause - it returns all the documents from all experience subcollections without any filtering (there can potentially be millions of those at this point).

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Maksym B.
  • 61
  • 5
  • This query is looking for userId field in the sub-document. The converter just restructures the object you get. Also, querying based on document ID in a collection group query is not supported at the moment. It'll be best to add userId as a field in the sub-documents. Checkout [this answer](https://stackoverflow.com/q/56188250/13130697) for more information. – Dharmaraj Jun 14 '22 at 16:45
  • 2
    You will need to add the document ID as a field of each experience document, then use that field as a filter in your collection group query. – Doug Stevenson Jun 14 '22 at 16:47

0 Answers0