title: How can I merge results into a single JSON while querying data from two different models in node.js and mongoose?
body: <p>I have two <code>mongoose</code> models in my app:</p>
  
  <pre><code>var UsersSchema = new Schema({
      username: {type: String},
      facebook_username: {type: String},
      display_name: {type: String}
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>var CommentsSchema = new Schema({
      user_id: {type: String, required: true},
      text_content: {type: String},
      photo_content_url: {type: String}
      comment_date: {type: Date, default: Date.now},
      created_at: {type: Date, default: Date.now},
  }
  </code></pre>
  
  <p>currently each single <code>comment</code> contains <code>user_id</code> - a direct link to its author.</p>
  
  <p>I created a <code>node.js</code> endpoint that takes <code>POST</code> query and returns JSON with all details about comments:</p>
  
  <pre><code>commentsRoutes.post('/comments', function (req, res) {
  
      var startDate = req.body.startDate;
      var endDate = req.body.endDate;
  
      var query= {};
      query.$and = [];
  
      // and condition on start date
      if(startDate != undefined) {
          var startDate = new Date(req.param('startDate'));
          var endDate = new Date(req.param('endDate'));
          query.$and.push({"comment_date":{$gte: startDate}});
          query.$and.push({"comment_date":{$lte: endDate}});
      }
  
      var finalquery = Comments.find(query)
      finalquery.exec(function(err, comments){
          if(err) {
              res.send(err);
              return;
          }
          res.json(comments);
      });
  });
  </code></pre>
  
  <p>This endpoint returns me a JSON with all the comments, however - I need to attach to each JSON details about its author - username, facebook_username and display_name (fetched based on <code>user_id</code> from <code>UsersSchema</code>). Can you tell me how could I do it?</p>
  
  <p><code>user_id</code> in <code>CommentsSchema</code> is a <code>mongoose id</code> of a specific user from <code>UsersSchema</code> </p>
  
  <p><strong>====== EDIT</strong></p>
  
  <p>Like @Antonio suggested in his answer below, I used <code>populate</code> in my case, it worked well and I saw merged JSON at the end. All user details were added, it looked like:</p>
  
  <pre><code>{ 
    "text_content": "blah",
    "photo_content_url": "http://www...",
    "comment_date": "some date",
    "created_at": "some date",
    "user_id": { "username": "someUsername",
                 "facebook_username": "fbUsername",
                 "display_name": "sth" }
  }
  </code></pre>
  
  <p>however - is there a way to include <code>POST</code> parameters in my endpoint that will apply to the <code>user_id</code> json?</p>
  
  <p>Currently I'm sending a <code>POST</code> parameters <code>startDate</code> and <code>endDate</code>. But if I send also <code>facebook_username</code> - how can I include it in a query and find only comments that were written by this author?</p>
  
  <p>I tried adding a simple condition:</p>
  
  <pre><code>var fb_username = req.body.facebookUsername;
  query.$and.push({"facebook_username": fb_username});
  </code></pre>
  
  <p>but it didn't work because there's no such field <code>facebook_username</code> in <code>CommentsSchema</code>...</p>
  
  <p>So how can I include condition attached to fields from UsersSchema that will limit results from CommentsSchema?</p>