title: Array size function in mongodb
body: <p>I'm beginner in MongoDB.
  I want to get an array size.
  I used as below:</p>
  <pre><code>db.users.aggregate([ 
  { $match: { id : &quot;test_id&quot; } },
  { $project : {
      id : 1, size : { $cond : {
              if : {$isArray: &quot;$sentence&quot;},
              then :{$size: &quot;$sentence&quot;},
              else: 0
              }
          }
      }
  }
  </code></pre>
  <p>])</p>
  <p>and I checked this</p>
  <pre><code>{&quot;id&quot; : &quot;test_id&quot;, &quot;size&quot; : 4 }
  </code></pre>
  <p>but I want to use this in a function as below:</p>
  <pre><code>    function getSentenceNumber(in_id) { 
      var ret = db.users.aggregate([ 
          { $match: { id : in_id } },
          { $project : { 
              id : 1,
              size : {$cond : { if : {$isArray: &quot;$sentence&quot;}, then : {$size: &quot;$sentence&quot;}, else: 0 } }
          } }
      ]); 
      return ret.size;
  }
  </code></pre>
  <p>To use db.users.update()</p>
  <p>But I got &quot;undefined&quot;.</p>
  <p>Would someone help me?</p>