MQLN
MQLN

Reputation: 2328

Query via collectionGroup by name

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?

Upvotes: 0

Views: 570

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

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.

Upvotes: 2

Related Questions