title: firebase firestore: Performing SQL-like joins
body: <p>I'm new to firebase but I have a <code>users</code> collection in which each user document will have the following fields</p>
  <ul>
  <li><code>username</code></li>
  <li><code>avatar</code> (the profile picture)</li>
  <li><code>name</code></li>
  </ul>
  <p>Each user can post a comment which will be stored inside a <code>comments</code> collection with these fields</p>
  <ul>
  <li><p><code>createdAt</code></p>
  </li>
  <li><p><code>username</code> (the user who commented)</p>
  </li>
  <li><p><code>userAvatar</code> (the user's avatar)</p>
  </li>
  </ul>
  <p>when it comes to retrieving the comments I can display every single comment with the user who commented (<code>username</code>) and the <code>userAvatar</code>. The problem though each comment has static <code>username</code> and <code>userAvatar</code> fields and if I were to change the user's avatar inside a user document, I'd like to modify the userAvatar inside the related comment document too for more data consistency.
  To achieve this I thought about creating a cloud function trigger to listen to the user's <code>avatar</code> update for changing the comment <code>userAvatar</code> accordingly. but I quickly realized it was a bad move in terms of cost since the cloud function would be triggered each and every time a change occurs inside the user's document regardless of if it is an <code>avatar</code> change or not.</p>
  <p>So I decided to create a user reference inside the comment document since it works like referenced keys in SQL databases like this</p>
  <ul>
  <li><code>createdAt</code></li>
  <li><code>user: db.doc('users/' + firebase.auth().currentUser.uid)</code></li>
  </ul>
  <p>Now the main issue is that if I wanted to retrieve let say 15 comments from the database, for each and every comment we will also have to get the user informations. so we'll have something like this</p>
  <pre><code>exports.getComments = async (req, res) =&gt; {
  try {
    let comments = [];
    const data = await db
     .collection(&quot;comments&quot;)
     .orderBy(&quot;createdAt&quot;, &quot;desc&quot;)
     .get();
    data.forEach((doc) =&gt; {
      let comment = doc.data();
      doc.data().user
       .get()
       .then(element =&gt; {
          comment.avatar = element.data().avatar;
          comment.username = element.data().username;
          comments.push(comment);
       })
      .catch(e =&gt; console.log(e));
    });
   return res.json(comments);
  } catch (e) {
      console.log(e);
      return res.status(500).json({ e });
  }
  };
  </code></pre>
  <p>now I'm wondering what if I had 1000 comments? in terms of latency, the app will not provide a good user experience. isn't it possible to retrieve data from both user and comment collection at once?</p>