title: Firestore Rules, allow update only certain fields
body: <p>I have the following database collection <code>'/player_profiles/{userId}'</code>.  I want to use the rule that only the <code>request.auth.uid == userId</code> unless they are only updating the field matches which is an array.  I tried this, but it denied permissions</p>
  
  <pre><code>match /player_profiles/{userId}{
          allow write: if userId == request.auth.uid; 
        allow update: if (request.resource.data.keys().hasOnly(["matches"]) &amp;&amp; request.auth != null);
      }
  </code></pre>
  
  <p>And here is the Flutter code that runs the update to add in the item to the matches array:</p>
  
  <pre><code>await DatabaseProvider()
          .db
          .collection(PLAYER_COLLECTION)
          .document(widget.userProfile.userId)
          .updateData({
        'matches': FieldValue.arrayUnion([profile.userId])
      });
  </code></pre>
  
  <p>Im new to firestore rules but i thought this would work</p>
  
  <p>Was able to get the behavior I wanted with this:</p>
  
  <pre><code>match /player_profiles/{userId}{
          allow write: if request.auth.uid == userId || (request.auth != null &amp;&amp; request.resource.data.diff(resource.data).affectedKeys().hasOnly(["matches"]));
      }
  </code></pre>
  
  <p>Looking at @Doug Stevenson's answer though I can add in making sure they only are allowed to put in their own userId into other peoples profiles.</p>