Also retrieving document_id with a Firestore query

I'm very much a beginner at Retool and coding in general, and can't find a simple way to get the document_id included in my Firestore query results.

I've an App built with screens using data from queries of Firestore. But the user collection in Firestore does not have a userId field, rather the unique user id is the document_id. I'd therefore like the document_id available in my Retool app query results. Would Event Handlers / Success be the place for script to add the document_ids?

Hi @Nico,

Welcome to the community!

To include the document_id in your Firestore query results, let's try to the following:

  1. Modify your Firestore Query: Within your Firestore query, you can add a projection field to explicitly include the document_id. This projection field should be named "name". For instance, if your current query retrieves documents from a collection named "users", you would modify it as follows:
db.collection('users').select('__name__').get().then((querySnapshot) => {
  // Process query results here
});
  1. Access the document_id in Retool: Once you've modified your query to include the document_id, you can access it directly within your Retool app. The document_id will be available as a property of each document object in the query results. For example, if you're using a Table component to display the query results, you can access the document_id using the following expression:
{{ data.documentId }}

This expression will display the document_id for the corresponding row in the table.

Regarding the use of Event Handlers / Success, you can certainly use them to add custom logic to your application, but they are not specifically required for retrieving the document_id. As described above, you can directly access the document_id property of the document objects in your query results.

Hope this helps.

:grinning:

Patrick

Hi Patrick, delighted to have some help! I'm still struggling though, as I can't see how to modify my initial query which is done inside the Retool App build, and I can't find how to add a projection field? Also, I'm after pulling back all user documents, is this what adding the projection "name" field will do or will it act like a where condition? Thx again Nico.

Hi @Nico,

Can you share a screenshot of a query that you're working with? I think that would help in moving towards a solution :crossed_fingers: