To know which document is changed in Firestore, for example, by their ID, please use the following lines of code:
FirestoreDb db = FirestoreDb.Create("myapp-123");
CollectionReference customersRef = db.Collection("customers");
FirestoreChangeListener listener = customersRef.Listen(snapshot =>
{
Console.WriteLine("Customer IDs:");
foreach (DocumentSnapshot documentSnapshot in snapshot.Documents)
{
Console.WriteLine(documentSnapshot.Id);
}
});
Please also note, that the snapshot handler will receive a new snapshot every time the query results change (that is when a document is added, removed, or updated).
Edit:
According to your comment:
it just prints out everything
That's the expected behavior since when you attach a snapshot listener to get updates in real-time, you are getting at the beginning all documents that satisfy the corresponding query. Unfortunately, there is no way you can change that. There is a workaround, however, which is explained in my answer from the following post:
How to skip initial data and trigger only new updates in Firestore Firebase?
Where you can see the actual changes to query results between query snapshots:
FirestoreDb db = FirestoreDb.Create("myapp-123");
CollectionReference customersRef = db.Collection("customers");
FirestoreChangeListener listener = customersRef.Listen(snapshot =>
{
foreach (DocumentChange change in snapshot.Changes)
{
if (change.ChangeType.ToString() == "Added")
{
Console.WriteLine("New customer: {0}", change.Document.Id);
}
else if (change.ChangeType.ToString() == "Modified")
{
Console.WriteLine("Modified customer: {0}", change.Document.Id);
}
else if (change.ChangeType.ToString() == "Removed")
{
Console.WriteLine("Removed customer: {0}", change.Document.Id);
}
}
});
But please remember that you cannot skip the "Added" case.
Also, since it's printing out everything every time a small change is made, does it mean if I have 100k documents I'll be getting charged for at least 100k reads whenever the listener fires?
No, it prints everything the first time you attach the listener and not at every change. Once you get all the documents that satisfy the query, you are only getting the documents that satisfy a particular case. However, if you have 100k documents in your collection, first lime you attach a listener you'll be billed with 100k reads. That's why you should consider implementing filters, or pagination.