Firestore: How to get updates for removed docs without download entire collection

3,851 views
Skip to first unread message

Kristian

unread,
Dec 4, 2017, 11:59:55 PM12/4/17
to Firebase Google Group
Hello.

I remember being able to get updates for removed children in Firebase without having to download all the children. It would be something like this:


// Get a reference to our posts
var ref = db.ref("server/saving-data/fireblog/posts");

// Get the data on a post that has been removed
ref.on("child_removed", function(snapshot) {
 
var deletedPost = snapshot.val();
  console
.log("The blog post titled '" + deletedPost.title + "' has been deleted");
});


This does not download anything under 'posts'. You will only receive updates for removed children of 'posts'.


I can't see how to do this efficiently in Firestore. Only thing I can think of is setting a listener for the entire collection and then filtering for removed documents. Something like this:

db.collection("cities")
   
.onSnapshot(function(snapshot) {
        snapshot
.docChanges.forEach(function(change) {
           
if (change.type === "removed") {
                console
.log("Removed city: ", change.doc.data());
           
}
       
});
   
});

But this would download all cities and then get every update.

Thanks,
Kristian



Samuel Stern

unread,
Dec 5, 2017, 11:40:12 AM12/5/17
to fireba...@googlegroups.com
Hi Kristian,

First it's wort clarifying that the behaviors in Realtime Database and Cloud Firestore are more similar than you think.  When you do "ref.on("child_removed")" the client was still listening to and downloading all changes to "ref" but your listener was only being fired for removed events.  It's identical to the Cloud Firestore example below.

However there are some ways you can achieve the actual behavior you're looking for:
  1. Trigger a Cloud Function when a document is deleted (docs)
  2. Rather than deleting docs completely, just add a "deleted: true" property and then listen on the query "db.collection("cities").where("deleted", "==", true)"
Hope that makes sense!
Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/bc04710d-0dbc-4c15-8532-1c173bf81aaa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ted Hwang

unread,
Jul 24, 2019, 10:05:33 AM7/24/19
to Firebase Google Group
For future people coming across this topic, here's firestore documentation that should solve the problem: https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots
Reply all
Reply to author
Forward
0 new messages