0

I

I have my firestore collection structure as shown in the image. Following is the main collection name which contains currently logged userId as a document ("AuScbj..") which contains a subcollection called uIds which contains the user ids of users he follows.

When logged in user ("AuScbj..") visits a particular user profile, how can I check whether that profile user's Id available in his following list just by querying like below

firebaseFirestore.collection("Following)
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection("uIds").where(
Mithun S
  • 408
  • 8
  • 20
  • So basically you want to check whether the logged-in user "AUScbTj5MpNY.." ID already exists in the uIds array of all users, right? – Alex Mamo Jun 28 '21 at 15:47
  • all other users which "AUScbTj5MpNY.." follows will be added in uIds array(id1,id2), so when "AUScbTj5MpNY.." visits a particular user profile (id1) it will check whether id1 available in his uIds array, if available that profile is marked as followed infront of "AUScbTj5MpNY.." – Mithun S Jun 29 '21 at 05:57

2 Answers2

0

You're looking for .where(fieldPath: "uids", opStr: "array-contains", value: followingUID). It should work with your simple "array" data.

LeadDreamer
  • 3,303
  • 2
  • 15
  • 18
  • So firebaseFirestore.collection("Following) .document(FirebaseAuth.getInstance().getCurrentUser().getUid()) .collection("uIds").where(fieldPath: "uids", opStr: "array-contains", value: followingUID) is that correct? can i refer the uIds as a sub collection like ...collection("uIds") – Mithun S Jun 29 '21 at 06:02
  • "Arrays" and collections/subcollections are COMPLETELY DIFFERENT. ```firebaseFirestore.collection("Following) .document(FirebaseAuth.getInstance().getCurrentUser().getUid())where(fieldPath: "uids", opStr: "array-contains", value: followingUID)``` – LeadDreamer Jun 29 '21 at 17:15
0

To check if id1 is present inside the uIds array in a really simpler manner, first, you should create a class:

class Document {
    List<String> uIds;
}

Then get the "AUScbTj5MpNY.." document and read the content of the uIds array using the following method:

private void checkId(String id) {
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    CollectionReference followingRef = rootRef.collection("Following");
    DocumentReference uidRef = followingRef.document(uid);
    uidRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    List<String> uIds = document.toObject(Document.class).uIds;
                    if (uIds.contains(id)) {
                        //Update the UI
                    }
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });
}

Inside the onComplete, we get the array as a List and check if the id with which we are calling the method, exists inside the array. To make it work, kick it off with:

checkId(id1);

Please also note that:

firebaseFirestore.collection("Following)
    .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
    .collection("uIds").where(/* ... /*);

Will never work, as uIds is an array inside a document and not a collection.

You can also find more info, in the following article:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Do you think this query is efficient? Suppose uIds having 1000 items, in you approach you are querying every records and filtering it. I need an efficient approach. – Mithun S Jun 29 '21 at 09:20
  • Yes, definitely is efficient! That's not about the number of items you have in the array, it's more about the [size of the document](https://stackoverflow.com/a/53569316/5246885). So as long as you keep the size under the 1MiB limitation, everything will work perfectly fine. – Alex Mamo Jun 29 '21 at 09:40