4

I have a list of users UIDs in an ArrayList.

Example:

List<String> myUsersUids = new ArrayList<>();

Now I want to query all users documents where their UID field's value exists within myUsersUids list.

Edit: Is that possible to do using "Where" conditions? so I can avoid looping on the list and query each document separately..

example: Is there something like..

    .whereExistsIn("userID", myUsersList) .. ?

This will help me add one SnapShotListener to multiple documents, instead of looping and creating a Listener for each document.

Thanks is advance!

Abuzaid
  • 853
  • 8
  • 28

1 Answers1

1

You can use the firestore.getAll method to get all the firebase documents with given document references, you can simply create the firebase document references by using document ids (for now: userIds ). Following is the Javascript code to get users with the given userIds:

let userIds = ['7Zzf082ExRLAWCJ6XZeE','random','OM3YmGKnPk2yZGEe2J02','random22'];
let userRefs = [];

userIds.forEach(qid => {
    console.log("Creating document reference of : ",qid);
    userRefs.push(admin.firestore().doc('users/'+qid));
});

admin.firestore().getAll(userRefs).then(users => {
    for(let i=0;i<users.length-1;i++){
        console.log("index : "+i+ " isExists : "+ users[i].exists);
    }
}

I took reference from https://stackoverflow.com/a/48423626/5140519 .

Please let me know if you have any question or if you have any feedback regarding this. Thank you! happy coding :)

Sunil Poudel
  • 109
  • 1
  • 5