title: How to join multiple documents in a Cloud Firestore query?
body: <p>I have a Cloud Firestore DB with the following structure:</p>
  
  <ul>
  <li>users
  
  <ul>
  <li>[uid]
  
  <ul>
  <li>name: "Test User"</li>
  </ul></li>
  </ul></li>
  <li>posts
  
  <ul>
  <li>[id]
  
  <ul>
  <li>content: "Just some test post."</li>
  <li>timestamp: (Dec. 22, 2017)</li>
  <li>uid: [uid]</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>There is more data present in the actual DB, the above just illustrates the collection/document/field structure.</p>
  
  <p>I have a view in my web app where I'm displaying posts and would like to display the name of the user who posted. I'm using the below query to fetch the posts:</p>
  
  <pre><code>let loadedPosts = {};
  posts = db.collection('posts')
            .orderBy('timestamp', 'desc')
            .limit(3);
  posts.get()
  .then((docSnaps) =&gt; {
    const postDocs = docSnaps.docs;
    for (let i in postDocs) {
      loadedPosts[postDocs[i].id] = postDocs[i].data();
    }
  });
  
  // Render loadedPosts later
  </code></pre>
  
  <p>What I want to do is query the user object by the uid stored in the post's uid field, and add the user's name field into the corresponding loadedPosts object. If I was only loading one post at a time this would be no problem, just wait for the query to come back with an object and in the <code>.then()</code> function make another query to the user document, and so on.</p>
  
  <p>However because I'm getting multiple post documents at once, I'm having a hard time figuring out how to map the correct user to the correct post after calling <code>.get()</code> on each post's <code>user/[uid]</code> document due to the asynchronous way they return.</p>
  
  <p>Can anyone think of an elegant solution to this issue?</p>