0

I've read into the collectionGroup docs and have read this answer on getting collections from a specific document.

I'm looking for something similar, but where I list all of the sub collections that are referenced by documents within a specific collection.

For example, my ideal query would be something like db.collection('metrics').collectionGroup('days'), where metrics is the large collection that contains documents, each of which have a sub collection days.

  • 1
    I'm also going to tie this question to this [other thread](https://stackoverflow.com/questions/56934947/collectiongroup-within-a-certain-path) that discusses the same problem where they suggest adding the parent collection's name as a field in each document under the `/days` collections. – samthecodingman Feb 20 '23 at 23:28
  • I also think that this [resource](https://medium.com/firebase-developers/how-to-query-collections-in-firestore-under-a-certain-path-6a0d686cebd2) will help. – Alex Mamo Feb 21 '23 at 08:39

2 Answers2

0

I am the author of the answer you linked. I looked into the guard that prevented me from expanding that answer to cover both documents and collections. It is unfortunately still in place as of firebase v9.17.1.

As long as that guard remains in place, collection group queries may only search the whole database or be restricted to paths that fall under a particular document. A collection is not currently possible.

The workaround for this would be to nest the metrics collection under its own document. For the sake of efficiency, you will want to keep this collection ID and document ID as short as possible. In the example below, I have used 'g' as shorthand for 'group' and '1' to indicate 'group 1' (could also use 'm' for metrics). Note: If you intend to have more than 10 such groups, start using 01, 02, etc. now, otherwise the below query will return invalid results.

"/g": {
  "/1": {
    "/metrics": {
      "/docA": {
        "/days": { /* ... */ }
      },
      "/docB": {
        "/days": { /* ... */ }
      },
      "/docC": {
        "/days": { /* ... */ }
      }
    }
  }
}

Which would allow you to then restrict paths to that group using:

// note: legacy syntax
firebase.firestore().collectionGroup('days')
  .orderBy(firebase.firestore.FieldPath.documentId())
  .startAt("g/1"),
  .endAt("g/1\uf8ff")
  .get()
  .then((querySnapshot) => {
    console.log("Found " + querySnapshot.size + " docs");
    querySnapshot.forEach((doc) => console.log("> " + doc.ref.path))
  })
  .catch((err) => {
    console.error("Failed to execute query", err);
  })
samthecodingman
  • 23,122
  • 4
  • 30
  • 54
0

You mentioned getting collections under a document. This is a sub collections and the query is something like

db.collection('metrics').document('x').collection('days')

from this reference you can query the documents in the days sub collection under document x

db.collectionGroup('days')

this will query all documents in all days collections regardless of what document they are under.

Seamus
  • 1,107
  • 10
  • 22
  • Hi! I'm not interested in what document the collection is under, just that they are all in the 'metrics' collection. Ideally I want every doc in the metrics' collections sub collection as a collection group, – Guy Torbet Feb 21 '23 at 11:49
  • Sub collections are nested under documents. It is not possible to have a sub collection of another collection. to query all documents in a sub collection regardless of document that is a collectionGroup query. – Seamus Feb 21 '23 at 19:51
  • If the issue is that maybe you have 'days' as a sub collection somewhere else but in this query you only want to query metrics/days as group, but you don't want somethingelse/days, then I believe the issue here is that you should have named those sub collections differently. – Seamus Feb 21 '23 at 19:55