title: How to create a document with a subcollection within Firestore cloud function?
body: <p>Hi I'd like to create the following document with the <code>private</code> subcollection, via a cloud function trigger:</p>
  <p><a href="https://i.stack.imgur.com/YgOc4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YgOc4.png" alt="enter image description here" /></a></p>
  <p>This is what I have so far, but have no idea how to actually set a subcollection as well.</p>
  <pre class="lang-js prettyprint-override"><code>&quot;use strict&quot;;
  Object.defineProperty(exports, &quot;__esModule&quot;, {value: true});
  
  // Cloud Functions for Firebase SDK to create Cloud Functions + set up triggers.
  const functions = require(&quot;firebase-functions&quot;);
  
  // The Firebase Admin SDK to access Firestore.
  const admin = require(&quot;firebase-admin&quot;);
  admin.initializeApp(functions.config().firebase);
  
  // Listen to .onCreate trigger
  exports.createProfile = functions.auth.user().onCreate((user) =&gt; {
    return admin.firestore().doc(`/users/${user.uid}`).set({
      firstName: null,
      lastName: null,
      preferredName: null,
    });
  });
  
  </code></pre>