Home > Software design >  Get latest Field from Firestore Collection using a Structured Query?
Get latest Field from Firestore Collection using a Structured Query?

Time:05-25

I am trying to use Zapier to add an email to a SendFox mailing list when a new user gets added to a specified Firestore path. It's asking me for a structured query to find this data. I am using the one that it's suggesting, but new users aren't being added correctly. My concern is that the structured query isn't set up correctly.

My data is structured as follows:

- Customers (Collection)
   - [User ID] (Document)
      - EMAIL
      - Other Info
      - Other Info...

Whenever a new UserID document is added, I'm trying to access the email field in Zapier.

This is the current structured query: enter image description here

   "orderBy": [{
       "field": {
           "fieldPath": "email"
       },
       "direction": "DESCENDING"
   }]

What is the correct structured query (json) to access the document I'm looking for? To clarify, it's the latest document in the "customers" collection with the field "email".

I'm not looking fo any javascript code to get this data, merely the correct json structure for this query.

Thank you!

CodePudding user response:

The query you have right now returns all documents from the customers collection in descending order of their email address, so with the zs first. That seems to be unlikely what you want.

Firestore has no built in concept of the most recent document, so what you'll want to do is:

  1. Add a timestamp field to each document that you set to the current time (preferably a server-side timestamp, but client-side will probably work too).
  2. Sort the query on descending values of that new timestamp field.
  • Related