Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Firebase Database V9 Return last item

I have a chat system where a chat contains multiple messages the structure looks like this below:

 - messages
   - ChatId123
      - MesageId123
      - MessageId456

I just want to retrieve the last message sent in this case MessageId456 I assume I need to use orderByKey(). The keys are generated using push() how can I correctly query and filter for the last item?

const query = query(ref(db, `messages/ChatId123`), orderByKey())
return query
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

the easiest way would be to add timestamp field in the message object like "SendAt" and orderBy this value :


const q = query(ref(db, `messages/ChatId123`), orderBy("SendAt", "desc"), limit(1));


example Firestore side

about 4 years ago · Juan Pablo Isaza Report

0

You can use limitToLast to get just the last item(s) from a query:

query(ref(db, `messages/ChatId123`), orderByKey(), limitToLast(1))

For more on this, see the Firebase documentation on filtering data.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!