2

I have a trigger on update document in firestore cloud function, in this function I want to get documents that contain a specific string. so I tried this:

const chat_threads = admin.firestore().collectionGroup('chatThreads').where(admin.firestore.FieldPath.documentId(),'array-contains', reciever_id).get().then(function (querySnapshot) {
    querySnapshot.forEach((doc) => {
      console.log(doc.id, " => ", doc.data());
    });
});

but I get this error

Error: Invalid Query. You can't perform 'array-contains' queries on FieldPath.documentId().

Is there another way to get the result? or what can I use instead of array-contains

Hanan Alhasan
  • 298
  • 3
  • 15
  • 1
    ```admin.firestore.FieldPath.documentId()``` is the document id. It's a string, not an array. Can you tell us more about what you're trying to do? It looks as if you're trying to get all the documents in your collection that has a specific ID. But if that's the case, why not just retrieve that document by itself using the ID? You wouldn't need the ```where``` query because you already know the ID – SRR Dec 28 '21 at 11:56
  • I'm trying to get all documents that contains a specific substring, I didn't know at first that it is wrong to use array-contains, but now it is clear to me, thanks for the clarification. @S.Ramjit – Hanan Alhasan Dec 28 '21 at 15:56

1 Answers1

1

I want to get documents that contain a specific string

Firestore doesn't have a way to find documents with arbitrary substrings. It can do prefix filtering (so a string that starts with a specific value), but no other substring matching.

This has been covered a few times before, so have a look at:

While these are not specific to document IDs (as you need), the same limits (and solutions/workarounds) apply here.

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