从mongo中的多个集合中获取数据

我有一些集合,如下所示是持有关系, testMastertestDoc之间的关系是在testDocMaster

例如:

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" } 

对于每个主入口,我们都期待着很多关系,

如果我有masterId,从testDoc表中获取数据的最好方法是什么。

假设你的testDocMaster模式使用ref其他两个集合的ObjectIdtypes,你可以使用Mongoose的查询填充支持来解决这个问题:

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

我得到它使用这个工作:

 // 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 }); }); }); }); 

现在,由于某种原因,当节点重新启动,我刷新,它不会呈现HTML到前端。 但是,任何后续刷新都会正确显示页面。