title: Mongodb | populate post with comment
body: <p>I'm making a post/comment system where I want users to be able to comment on posts (called 'captures' in my case).</p>
  
  <p>I currently have everything setup except for the populating of the posts.</p>
  
  <p>I can add comments on a seperate document and I got them refrenced, but when I try to add the populate methode, it doesn't do anything:</p>
  
  <pre><code>router.get('/captures/:capture', function(req, res){
           Capture.findOne({_id: req.params.capture}, function(err, data){
              if(err) 
                  throw err;
               req.capture.populate('comments');   
               res.json(data);
          });
       });
  </code></pre>
  
  <p>When doing this, it doesn't give any errors, but it also doesn't populate.
  I simply get the id's of the comments, but it doesn't populate the 'capture'.</p>
  
  <pre><code>console.log($scope.capture.comments);
  </code></pre>
  
  <p>gives:</p>
  
  <pre><code>["5744bafc1460f5bf18c17ac0", "5744bd4cfcd57403198f0987"]
  </code></pre>
  
  <p>This is my full code:</p>
  
  <p><strong>capture.js route:</strong></p>
  
  <pre><code>var Capture = require('../models/capture');
  var Comment = require('../models/comment');
  
  module.exports = function(router) {
      router.post('/captures', function(req, res){
          var capture = new Capture();
          capture.birdname = req.body.birdname;
          capture.place =  req.body.place;
          capture.userId = req.body.userId;
          capture.author = req.body.author;
          capture.picture = req.body.picture;
          capture.created_at = new Date();
  
  
          capture.save(function(err, data){
              if(err)
                  throw err;
              console.log(req.body);
              res.json(data);
          });
      });
  
      // Map logic to route parameter 'capture'
      router.param('capture', function(req, res, next, id) {
          var query = Capture.findById(id);
  
          query.exec(function (err, capture) {
              if (err) { return next(err); }
              if (!capture) { return next(new Error("can't find post")); }
  
              req.capture = capture;
              return next();
          });
      });
      // Map logic to route parameter 'comment'
      router.param('comment', function (req, res, next, id) {
          var query = Comment.findById(id);
  
          query.exec(function (err, comment) {
              if (err) { return next(err); }
              if (!comment) { return next(new Error("can't find comment")); }
  
              req.comment = comment;
              return next();
          });
      });  
  
      router.get('/captures/:capture', function(req, res){
           Capture.findOne({_id: req.params.capture}, function(err, data){
              if(err) 
                  throw err;
  
  ==========&gt; As you can see here, I'm trying to populate the given id 
  ==========&gt; with the comments. Currently when I console log on my capture,
  ==========&gt; it only gives back the id, but I want it to be populated with
  ==========&gt; the corresponding comments.
  
               req.capture.populate('comments');   
               res.json(data);
      });
   });
  
      router.post('/captures/:capture/comments', function(req, res, next){
          var comment = new Comment();
          comment.body =  req.body.body;
          comment.userId = req.body.userId;
          comment.author = req.body.author;
          comment.created_at = new Date();
          comment.capture = req.capture;
  
          comment.save(function(err, comment) {
              if (err) { return next(err); }
  
              req.capture.comments.push(comment);
              req.capture.save(function(err, capture) {
                  if (err) { return next(err); }
  
                  res.json(comment);
              });
          });
      });
  };
  </code></pre>
  
  <p><strong>capture model:</strong></p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  var captureSchema = Schema({
      birdname: {type: String, required: true},
      place: String,
      userId: String,
      author: String,
      picture: Schema.Types.Mixed,
      created_at: Date,
      comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment'}]
  });
  
  module.exports = mongoose.model('Capture', captureSchema);
  </code></pre>
  
  <p><strong>comment model:</strong></p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  var commentSchema = Schema({
      body: String,
      userId: String,
      author: String,
      created_at: Date,
      capture: [{ type: Schema.Types.ObjectId, ref: 'Capture'}]
  });
  
  module.exports = mongoose.model('Comment', commentSchema);
  </code></pre>