title: Getting duplicate results on firestore query?
body: <p>I am trying to display posts of users you are following, including your posts.</p>
  <p>First, I have a collection of all the users the current user is following. I get this collection:</p>
  <pre><code>Firebase.firestore()
  .collection('following')
  .doc(Firebase.auth().currentUser.uid).collection('following');
  </code></pre>
  <p>This returns the collection of users that the current user is following. Now, I need to pull each post for each user using this query:</p>
  <pre><code>    querySnapshot.forEach(async (res) =&gt; {
          await Firebase.firestore()
          .collection('globalPosts')
          .where(&quot;uid&quot;, &quot;in&quot;, [res.data().uid, Firebase.auth().currentUser.uid])
  </code></pre>
  <p>I had the logic separated (first push the current user's posts to the array, then go one by one through each &quot;following&quot; user and pull their posts).</p>
  <p>But now I want to do lazy loading for these posts, so decided to combine the query into one.
  This has caused the current user's posts to be duplicated. How can I remove the duplicates?</p>
  <p>EDIT: this is how I was doing it originally, First getting all the current users posts, then each of the following users posts one by one which worked with no duplicates</p>
  <pre><code> await Firebase.firestore()
      .collection('globalPosts')
      .where(&quot;uid&quot;, &quot;==&quot;, Firebase.auth().currentUser.uid)
       .onSnapshot(function(query) {
          query.forEach((doc) =&gt;  {
               const { 
                   ..Fields
                  } = doc.data();
      
                  followingPosts.push({
                  ..Fields
                   });
           })
      
       });
  
      querySnapshot.forEach(async (res) =&gt; {
          await Firebase.firestore()
          .collection('globalPosts')
          .where(&quot;uid&quot;, &quot;in&quot;, res.data().uid)
          .onSnapshot(function(query) {
              query.forEach((doc) =&gt;  {
                  const { 
                      ..Fields
                      } = doc.data();
  
          
                      followingPosts.push({
                      ..Fields
                      });
                  
              })
              followingPosts.sort(function(a,b){ 
  
                  return b.date_created.toDate() - a.date_created.toDate()
          
              })
          
          });
  
      });
  
      this.setState({
          followingPosts,
          isLoading: false,   
      });
  </code></pre>