title: Can I "populate" a property inside an array of sub-documents using MongooseJS?
body: <p>There are many SO questions about mongoose's populate feature, but I've been unable to find evidence of whether this is possible, and if so, what I am doing wrong.</p>
  
  <p>Let's say I'm making a blog. Yay.  All users can write articles and post comments on other articles.  I chose to model things this way:</p>
  
  <p>Users:</p>
  
  <pre><code>var UserSchema = new Schema({
      name: String,
      email: {
          type: String,
          unique: true
      }
  });
  mongoose.model('User', UserSchema);
  </code></pre>
  
  <p>Articles with embedded comments:</p>
  
  <pre><code>var ArticleSchema = new Schema({
      title: {
          type: String
      },
      createdBy: {
          type: Schema.ObjectId,
          ref: 'User'
      },
      comments: [
          commentsSchema
      ]
  )};
  
  var commentSchema = new Schema({
      message: {
          type: String
      },
      createdBy: {
          type: Schema.ObjectId,
          ref: 'User'
      }
  });
  
  mongoose.model('Article', ArticleSchema);
  </code></pre>
  
  <p>When I want to load an article, I can use mongooseJS <a href="http://mongoosejs.com/docs/populate.html" rel="nofollow noreferrer">populate</a> to load an article with its creator like so:</p>
  
  <pre><code>Article
  .findOne({_id: articleid})
  .populate('createdBy', 'name email')
  .exec(function(err,article) {
      console.log('Created by %s', article.createdBy.name); // This works, yay.
  });
  </code></pre>
  
  <p>But if I want to also load the comments with THEIR creators using populate, comment.createdBy is null:</p>
  
  <pre><code>Article
  .findOne({_id: articleid})
  .populate('createdBy', 'name email')
  .populate('comments.createdBy', 'name email') //am I doing this correctly?
  .exec(function(err,article) {
      console.log('First comment by %s', article.comments[0].createdBy.name); // Error, createdBy is null.
  });
  </code></pre>
  
  <p>With that last version, I can see that article.comments[0].createdBy is null. I have verified that in the db the comment has a mongoose object ID for createdBy (and that it matches an existing user).</p>
  
  <p>Is this possible?<br>
   - If so, where am I going wrong?<br>
   - If not, what is a better way to go about this?  MongoDB questions like <a href="https://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference">this one</a> lead me to believe I'm on the right track, but perhaps mis-using mongoose?</p>