Fetch data from multiple collections in mongo

I have some collections shown like below which is holding relationships, relation between testMaster and testDoc is holding inside the testDocMaster

For eg:

testMaster {
_id: "Schema.Objectid",
name: "String" //master name
}

testDoc {
_id : Schema.ObjectId,
name: "String", //doc name
other datas
}

testDocMaster {
masterId: "_id of the testMaster table",
docId : "_id of the above testDoc"
}

For each master entry, we are expecting many relations,

what would be the best way to fetch the data from the testDoc table, if I have the masterId.

I got it working using this:

// GLOBAL ARRAYS FOR STORING COLLECTION DATA
var collectionOne = [];
var collectionTwo = [];
app.get('/', function(req, res){
  MongoClient.connect("mongodb://localhost:27017/michael", function(err, db) {
    if(!err) {
      console.log("We are connected");
    }
    db.collection("collectionOne", function(err, collection) {
      collection.find().sort({order_num: 1}).toArray(function(err, result) {
        if (err) {
          throw err;
        } else {
          for (i=0; i<result.length; i++) {
            collectionOne[i] = result[i];
          }
        }
      });
      db.collection("collectionTwo", function(err, collection) {
        collection.find().sort({order_num: 1}).toArray(function(err, result) {
          if (err) {
            throw err;
          } else {
            for (i=0; i<result.length; i++) {
              collectionTwo[i] = result[i];
            }
          }
        });
      });
      // Thank you aesede!
      res.render('index.html', {
        collectionOne: collectionOne,
        collectionTwo: collectionTwo
      });
    });
  });
});

Now, for some reason when Node restarts, and I hit refresh, it doesn't render HTML into the front-end. However, any subsequent refresh renders the page correctly.

Assuming your testDocMaster schema uses ObjectId types that ref the other two collections you can use Mongoose's query population support to help with this:

TestDocMaster.findOne({ masterId: masterId})
    .populate('docId')
    .exec(function(err, testDocMaster) {
        // testDocMaster.docId is populated with the full testDoc for the
        // matching _id
    });