title: How can I miminize read/wriites for my firestore project with groups that contains multiple documents and multiple users?
body: <p>I'm working on a personal project that uses Firestore to store recipes and the ability to create groups that contain several recipes and several users, and I want to structure my database to minimize excessive read/writes.</p>
  <p>Here is my current setup. I've omitted parts of the documents that are irrelevant:</p>
  <pre class="lang-js prettyprint-override"><code>// recipe-id-1
  recipe: {
    userID: 'user-id-1', // who the recipe owner is
    groups: ['group-id-1', 'group-id-2'], // groups this recipe is a part of
    users: { // all the users that have access to this recipe
      'user-id-1': {
        name: 'Alice',
        associatedGroups: ['group-id-1', 'group-id-2'] // what groups this user has access to the recipe through
      },
      'user-id-2': {
        name: 'Bob',
        associatedGroups: ['group-id-1']
      },
    }
  }
  
  // group-id-1
  group: {
    users: {
      'user-id-1': {
        ...
      },
      'user-id-2': {
        ...
      },
    }
  }
  </code></pre>
  <p>As you can see, all of my recipe querying is done directly on the recipe documents themselves. Here is my current problem: say I have 1000 recipes in a given group. If I add or remove a user from the group, then I have to update all 1000 recipes to modify the user's access to the recipe.</p>
  <p>Aside from that issue, this setup makes querying very convenient for two scenarios while also abiding by security rules that ensure I have permission to access the recipe:</p>
  <ul>
  <li><code>db.collection('recipes').where(`users.${myUserID}.name`, '&gt;', '')</code>
  <ul>
  <li>This allows me to perform a retrieval of all of the recipes I have access to, be it my own recipes or other users' recipes through groups I am apart of</li>
  </ul>
  </li>
  <li><code>db.collection('recipes').where(`users.${myUserID}.associatedGroups`, 'array-contains', 'group-id-1')</code>
  <ul>
  <li>This allows me to retrieve all of the recipes within a certain group</li>
  </ul>
  </li>
  </ul>
  <p>Here are the things my project requires:</p>
  <ul>
  <li>A way to perform a universal search across all possible recipes you have permission to access</li>
  <li>A way to create a group that has multiple users and multiple recipes, and set  recipe access permissions accordingly</li>
  </ul>
  <p>How can I achieve those goals without having to read/write 1000 times (in the example above) for each user?</p>