How to get newly updated data only from Firestore?

Viewed 2614

Always getting whole document while any changes found on Firestore. How to get data that are newly updated only?

This is my data:

collections

I need msg and sendby in order of object(for eg: 2018_09_17_30_40) inside chat on first load and get only the new msg and sendby if data updated while on get() getting whole document without any order Note: Code for Android app.

2 Answers

You can store the last fetched date, and only get the objects with date greater than last updated date.

db.collection('groups')
  .where('participants', 'array-contains', 'user123')
  .where('lastUpdated', '>', lastFetchTimestamp)
  .orderBy('lastUpdated', 'desc')

Always getting whole document while any changes found on fire store.

The new Cloud Firestore database has different concepts than Firebase real-time database and should not be confused. There are no field-level permissions or access to a document. It's the entire document, or nothing. Cloud Firestore listeners fire on the document level. There is no way to get triggered with just newly updated data in a document.

Seeing your database, I can say that you haven't chosen the right schema by adding those messages under the chat property.

According to the official documentation:

Cloud Firestore is optimized for storing large collections of small documents.

You should consider create a new collection named messages and store each message as a separate document. In the link above, there are examples of how you can achieve that.

Related