Get documents with query on document id in Cloud Functions and Firestore

Viewed 217

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

1 Answers

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.

Related