zakSyed
zakSyed

Reputation: 1404

How to get only updated document from Firestore

Is there a way to get only the updated document from firestore instead of all the documents when using valueChanges

return this.afs.collection('users').doc(userId).collection('chat_users').valueChanges();

I have the following code and it returns all the documents when only one has changed.

Upvotes: 0

Views: 620

Answers (1)

Tim Martens
Tim Martens

Reputation: 1534

You can use stateChanges() for this:

What is it? - Returns an Observable of the most recent changes as a DocumentChangeAction[].

Why would you use it? - The above methods return a synchronized array sorted in query order. stateChanges() emits changes as they occur rather than syncing the query order. ...

return this.afs.collection('users')
    .doc(userId)
    .collection('chat_users')
    .stateChanges(['modified'])
    .pipe(
        map((actions) => actions.map(a => {
            const data = a.payload.doc.data();
            const id = a.payload.doc.id;
            return { id, ...data };
        }))
    );

Upvotes: 1

Related Questions