title: Firebase customised rules
body: <p>As of now I have below rules defined for my <code>customer</code> table.</p>
  
  <pre><code>{
    "rules": {
      "customers":{
        ".read": "auth != null", 
        ".write": "auth != null", 
  
        "$CID":{
          "UserId":{
            ".validate": "(data.exists() &amp;&amp; data.val() == newData.val()) || newData.val() == auth.uid"
          },
          "CustomerName":{
            ".validate": "newData.isString() &amp;&amp; newData.val().length &lt; 100"
          },
          "CustomerCode":{
            ".validate": "newData.isString() &amp;&amp; newData.val().length&lt;4"
          },
          "CustomerLimit":{}
        }
      }   
    }
  }
  </code></pre>
  
  <p>As you can see, that I have <code>UserId</code> under <code>customers</code> branch which would hold the value of the logged in user id. Each authenticated User can create customers which basically belongs to that particular user and read/get only those <code>customers</code> which was created by him.</p>
  
  <p>But now when I read from database as below:</p>
  
  <pre><code>DatabaseReference mDatabaseReference= FirebaseDatabase.getInstance().getReference("customers");
  </code></pre>
  
  <p>This retrieves all the data under the customers. So I was thinking to add some <code>read</code> rule to the existing one as in </p>
  
  <pre><code>"rules": {
      "customers":{
        ".read": "auth != null &amp;&amp; auth.uid=loggedInUserId",  //something like this
      .....
      }
  }
  </code></pre>
  
  <p>But I just couldn't find anywhere how it can be done here in the <code>rules</code>. </p>
  
  <p>As an option I tried to write a <code>query</code> on <code>UserId</code> which always returned <code>null</code> even after passing valid logged in <code>UserId</code>.</p>
  
  <pre><code>Query query=mDatabaseReference.equalsTo(loggedInUserId,"UserId");
  </code></pre>
  
  <p>This method which I don't prefer as it would be best written in rules. Hope someone knows how we can add <code>rules</code> for this requirement.</p>