title: how to write mongodb query for 'counting elements in sub-document'
body: <p>I have collection something like this...</p>
  
  <pre><code>{
      {
          id: 123,
          name: "abc",
          data: {
              type1: 0.78,
              type2: 0.89,
              type3: 0.67
          }
          purpose: "commercial"
      }, {
          id: 124,
          name: "pqrs",
          data: {
              type1: 0.34,
              type2: 0.78,
          }
          purpose: "open"
      }, {
          id: 125,
          name: "xtec",
          data: {
              type1: 0.48,
              type2: 0.56,
              type3: 0.67
              type4: 0.56,
              type5: 0.67
          }
          purpose: "some other"
      }
  }
  </code></pre>
  
  <p>I would like to count the number of elements in "data" sub-document. Could any one post me mongodb query?</p>
  
  <p>or I would like to display all the documents where the "data" sub-document length = 3</p>
  
  <p>the below query doesn't work</p>
  
  <pre><code>db.test.find({$where : 'data.length = 3'});
  </code></pre>
  
  <p>Any help would greatly appreciated. Thank you!!</p>
  
  <p>Regards,
  Raj</p>