title: How can I create a mongoose virtual field that lists the length of a subdocument?
body: <p>I have a node.js REST api route which returns projects objects as json minus the tasks:</p>
  
  <pre><code>   exports.list = function(req, res) {
      return Project.find(null,'-tasks',null,function(err, projects) {
       if (!err) {
        return res.json(projects);
       } else {
        return res.send(err);
       }
      });
     };
  </code></pre>
  
  <p>This is intended to be used for navigation elements that list all projects but won't be needing the individual tasks (which number in the 100s). This works well but now I need to include a count of the number of tasks in the navigation so I added this to my model:</p>
  
  <pre><code>var ProjectSchema = Schema({
    name    : String,
    tasks : [{ 
      name : String,
      state   : String }]
  },
  {
      toObject: { virtuals: true },
      toJSON: { virtuals: true }
  });
  
  ProjectSchema.virtual("taskCount").get(function(){
      return this.tasks.length;
  });
  </code></pre>
  
  <p>But I get the following error: TypeError: Cannot read property 'length' of undefined</p>