0

So as of May 8 2019, Firebase Firestore now supports querying sub-collections of the same name via collectionGroup('collectionName').

Is there a way to retrieve multiple documents of the same docId across these sub-collections?

Here's a little visual example of what I'm trying to accomplish.

collectionA
  - documentA
    : collectionB
      - documentC
  - documentB
    : collectionB
      - documentC

Let's say I want to get both of those documentC documents. Is there a way to do so?

The current code I'm trying, to use is this:

const docIdFieldPath = FieldPath.documentId()
admin.firestore().collectionGroup('collectionB').where(docIdFieldPath, '==', 'documentC').get().then(...)

However I'm thrown an error of:

INVALID_ARGUMENT: Resource name "projects/myProject/databases/(default)/documents/documentB" lacks "/" at index 86.

The docs on the matter say to create an index before attempting querying via collectionGroup, but if I try to do so via the online-index-portal I get an error saying that the __name__ field is reserved.

Am I missing something silly, or is this feature not supported yet?

MQLN
  • 2,292
  • 2
  • 18
  • 33
  • I get that error too, but I don't know if this is an intended feature or not. FYI it appears you have an extra `collection("collectionA")` in your code. Is that a typo? I'd expect it not to work, as `collectionGroup()` is only a method on `Firestore`. You can't constrain a collection group query to a subcollections of a particular collection. – Doug Stevenson May 16 '19 at 02:32
  • Oops, good call @DougStevenson, that was a typo. But that's interesting to know that there's not a way to restrain `collectionGroups`! Are you saying that you're not sure on whether or not `collectionGroup` queries supporting `__name__` queries are a feature yet? Thanks! – MQLN May 16 '19 at 02:52
  • Possible duplicate of [Firestore collection group query on documentId](https://stackoverflow.com/questions/56149601/firestore-collection-group-query-on-documentid) – Alex Mamo May 16 '19 at 09:16

1 Answers1

2

Some of the Firestore team has told me that this isn't supported. You can't filter based on the string of the document ID. The mobile SDKs give you a nicer message, but apparently the nodejs SDK does not.

What you can do instead is store the id of the document as a field in the document itself, and filter on that field.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441