title: What Firebase rule will prevent duplicates in a collection based on other fields?
body: <p>I'm creating an application which lets users create items and then allow other users to subscribe to those items. I'm struggling to craft a rule that will prevent users from subscribing more than once to an item.</p>
  
  <p>Here is an example of my data structure (anonymized, hence the "OMITTED" values):</p>
  
  <pre><code>{
      "OMITTED" : {
          "name" : "Second",
          "body" : "this is another",
          "userName" : "Some User",
          "userId" : "OMITTED",
          "created" : 1385602708464,
          "subscribers" : {
              "OMITTED" : {
                  "userName" : "Some User",
                  "userId" : "OMITTED"
              }
          }
      }
  }
  </code></pre>
  
  <p>Here are my Firebase rules at present:</p>
  
  <pre><code>{
    "rules": {
      ".read": true,
      ".write": "auth != null",
      "items": {
        "$item": {
          ".write": "!data.exists()",
          ".validate": "newData.hasChildren(['name', 'body', 'userId', 'userName']) &amp;&amp; newData.child('userId').val() == auth.id",
          "subscribers": {
            "$sub": {
              ".validate": "newData.hasChildren(['userId', 'userName']) &amp;&amp; newData.child('userId').val() != data.child('userId').val()"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>How can I prevent users from subscribing more than once? What is the rule I need to prevent duplicate users within the <code>subscribers</code> list based on <code>userId</code>?</p>