5

Now we have collection group queries on firestore! Great.

But due to some rules, how can one query a collection group by Document ID?

I tried:

return this.afs.collectionGroup('tokens', (ref) => {
      return ref.where(firestore.FieldPath.documentId(), '==', `${this.afAuth.auth.currentUser.uid}`)
    }).valueChanges();

But I get:

Invalid third parameter to Query.where(). When querying a collection group by FieldPath.documentId(), the value provided must result in a valid document path, but 'JKq2AFqGkkSLHsIVaXz4l3wwmfh1' is not because it has an odd number of segments (1).

I also tried:

return this.afs.collectionGroup('tokens', (ref) => {
      return ref.where(firestore.FieldPath.documentId(), '==', `/users/JKq2AFqGkkSLHsIVaXz4l3wwmfh1`)
    }).valueChanges();

But then I get 0 results

My tokens collection document ID's are based on my /users/{userID}

The rules I have for the tokens collection are:

 match /{path=**}/tokens/{userID} {
            allow read, write, create, update, delete: if request.auth.uid == userID;
    }

For example this is the document I need to get

/suuntoAppAccessTokens/dimitrioskanellopoulos/tokens/JKq2AFqGkkSLHsIVaXz4l3wwmfh1

Of course if I use:

return this.afs.collectionGroup('tokens', (ref) => {
      return ref.where(firestore.FieldPath.documentId(), '==', `/suuntoAppAccessTokens/dimitrioskanellopoulos/tokens/JKq2AFqGkkSLHsIVaXz4l3wwmfh1`)
    }).valueChanges();

it works but that beats the point of the collection group queries

So is there any way to query a collection group by document ID and not path + document id ?

Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117

0 Answers0