2

I have a user collection, and within that collection there's a collection name groups, which then has groupItems. Is it possible to run a query for all groupItems for a specific user?

I could run a global version like:

db.collectionGroup('groupItems')

but that is overkill for me, I'm looking for something like:

db.collection('users').doc(uid).collection('groups').collectionGroup('groupIems').where('categoryOnly', '==', true)

Is this possible?

The architecture of the collections looks like this:

Users
---> User
------> Collections
---------> GroupA
------------> GroupAItems
---------------> GroupAItem1
---------------> GroupAItem2
---------> GroupB
------------> GroupBItems
---------------> GroupBItem1
---------------> GroupBItem2

Ideally I could call for all Group Items instead of first calling for groups and getting [GroupA, GroupB...], and then calling for GroupA's items, then GroupB's, etc.

Thingamajig
  • 4,107
  • 7
  • 33
  • 61
  • Did you just tried out if it's working? – Constantin Beer Sep 14 '19 at 16:50
  • Yeah. The call I have as-is is returning that the function does not exist. – Thingamajig Sep 14 '19 at 16:56
  • It sounds like you might want to reconsider the naming of your subcollections so that they're easier to query with a collection group query. As it stands now, your data requires multiple queries to get everything you need. – Doug Stevenson Sep 14 '19 at 17:22
  • I think the workaround is to add the user's uid to each group item, then a where condition looking for that uid on a collectiongroup query should work. – Thingamajig Sep 14 '19 at 17:25

1 Answers1

1

Update: it turns out that querying a specific path may be possible after all, thanks to how FieldPath.documentId() is indexed for collection group indexes. Check @samthecodingman's answer here: https://stackoverflow.com/a/68049847

Happy answer above


Old answer below

There is currently no way to limit a collection group to just collections under a specific path. All collection group queries get documents from all collections with the specified name.

So the only way to currently query a subset of your groups is to use a naming scheme that allows you to uniquely address them.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807