How would you implement a matching system to check when two values are the same in one collection from different documents? This code has to call a function to run every-time it detects that the two values are matching.
Asked
Active
Viewed 236 times
0
-
What have you tried so far? – Alex Mamo Nov 22 '21 at 09:46
-
Wouldn't this just be a matter of querying the collection for the value and if the returned snapshot count > 1 then there's a duplicate? Is there something more? – Jay Nov 22 '21 at 18:57
-
It has to find the duplicate value and then execute a function based on that document – chackerian Nov 23 '21 at 02:37
-
Right. So what's the issue with writing that code? Or did you attempt it and are having an problem with the code? Please take a moment and review [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Nov 24 '21 at 18:24
1 Answers
1
As mentioned in the documentation :
Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group. These queries > can also be used with either get() or addSnapshotListener(), as described in Get Data and Get Realtime Updates.
I will recommend you to use a unique identifier for each entry and then you can add a listener that will match each data from the collections. Syntax :
query.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { // dataSnapshot is the "issue" node with all children with id 0 for (DataSnapshot issue : dataSnapshot.getChildren()) { // do something with the individual "issues" } } }
You can refer to the Stackoverflow answer where Frank has explained it briefly with the following function code.
public interface AlreadyBookedCallback { void onCallback(boolean isAlreadyBooked); } private void alreadyBooked(final String boname, final String bodept, final String botime, AlreadyBookedCallback callback) { CollectionReference cref=db.collection("bookingdetails"); Query q1=cref.whereEqualTo("time",botime).whereEqualTo("dept",bodept); q1.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { @Override public void onSuccess(QuerySnapshot queryDocumentSnapshots) { for (DocumentSnapshot ds : queryDocumentSnapshots) { String rname, rdept, rtime; rname = ds.getString("name"); rdept = ds.getString("dept"); rtime = ds.getString("time"); if (rdept.equals(botime)) { if (rtime.equals(botime)) { isExisting = true; } } } callback.onCallback(isExisting) } }); }
For more information you can also refer to this thread on how to check duplicate values in documents.

Divyani Yadav
- 1,030
- 4
- 9