title: How can I filter documents in Firestore based on an array stored in another collection?
body: <p>I have a Firestore database organized like so:</p>
  
  <p><a href="https://i.stack.imgur.com/d7C6R.png" rel="nofollow noreferrer">FirestoreDB</a>
  <a href="https://i.stack.imgur.com/GCu6c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GCu6c.png" alt="FireStore DB"></a></p>
  
  <p>I have three fragments which display: all posts, posts of individuals that user is following and all the posts by the user. My issue is with getting the posts of individuals that user is following to work, the other two work.</p>
  
  <p>I previously have tried to get the array from Firestore and use that array to query the posts of followed users but I am unable to access the array outside of the method that generates it as described here: <a href="https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document</a></p>
  
  <p>Like so:</p>
  
  <pre><code>DocumentReference docRef = db.collection("following").document(currentUser.getUid());
          docRef.get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
              @Override
              public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
                  if (task.isSuccessful()) {
                      DocumentSnapshot document = task.getResult();
                      if (document.exists()) {
                          List&lt;String&gt; group = (List&lt;String&gt;) document.get("following");
                      } else {
                      }
                  } else {
                  }
              }
          });
  
  Query posts = db.collection("posts").whereEqualTo("userId", group);//Does not compile since cannot resolve group.
  </code></pre>
  
  <p>Where I then created an array using the <code>DocumentSnapshot</code> however since my query is outside the scope of the method I cannot use the created array.</p>
  
  <p>What I currently have that does not work is the following:</p>
  
  <pre><code>Query posts = db.collection("posts").whereEqualTo("userId", FirebaseFirestore.getInstance()
                  .collection("following")
                  .document(currentUser.getUid()));
  </code></pre>
  
  <p>Thank you for any help you can provide.</p>
  
  <p>final Issue</p>
  
  <pre><code>private void readData(MyCallback callback){
          DocumentReference docRef = db.collection("following").document(currentUser.getUid());
          docRef.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
              @Override
              public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                  if (task.isSuccessful()) {
                      for (DocumentSnapshot document : Objects.requireNonNull(task.getResult())){
                          String group = document.getString("following");
                          followingList.add(group);
                      }
                      callback.onCallback(followingList);//says must be declared final
                  } else {
                  }
              }
          });
      }
  </code></pre>