title: Append more properties to results retrieved from NodeJS (express) + MongoDB - simulate SQL join
body: <p>Can someone enlighten me with a solution for the following situation?</p>
  
  <p>I have a logging application written in nodeJS express framework and using MongoDB collections. I managed to get the results from a collection, but I want to iterate on the results, query another collection based on the reference ID from the first query and append more values to the initial results, then send the response to the client.</p>
  
  <p>I will paste bellow what I have now coded, but I cannot get it to work. I'm either doing it wrong or my desired logic is not the right one.</p>
  
  <p>Please help me with more than theory and the fact that I cannot use the results outside an asynchronous JS function.</p>
  
  <p>Thanks in advance!</p>
  
  <p>CODE:</p>
  
  <pre><code>exports.findLogs = function(req, res) {
      var params = req.body;
      var query = req.query;
      var coll = params.section;
      if (coll === undefined) {
          coll = 'traces';
      }
  
      db.collection(coll, function(err, collection) {
          collection.count(function(err, count) {
              var criteria = {
              'limit': limit,
                  'skip': skip,
                   'sort': [[options.sortname, options.sortorder]]
               }
               collection.find({}, criteria).toArray(function(err, items) {
                   Object.keys(items).forEach(function(logIndex) {
                       var trace = items[logIndex];
                       var crit = {
                           'find': {LogId: trace.LogId},
                           'projection': {},
                           'limit': 1
                       }
  
                       // Get entry based on Criteria from `log` table
                       findOneBy(crit, 'log', function(err, log) {
                           if (log.length !== 1) {
                               // no reference found
                           } else {
                                // Appending here to log DOES NOT stick as expected
                               trace.ComputerName = log.ComputerName;
                               trace.ComputerIP = log.ComputerIP;
                               trace.LastSeen = log.LastSeen;
                               trace.TestVar = 'test1234';
                           }
                       });
  
                       // Appending here to trace works as expected
                           trace.Actions = 'MyAction';
                   });
  
                   results['total'] = count;
                   results['results'] = items.length;
                   results['rows'] = items;
                   res.send(results);
              });
          });
      });
  }
  
  function findOneBy(criteria, coll, callback) {
      var cFind = criteria.find;
      var cProj = criteria.projection;
      db.collection(coll, function(err, collection) {
          if (err) return callback(err);
          else return collection.find(cFind, cProj).toArray(callback);
      });
  }
  </code></pre>