app.get('/api/wherecritique/reviews/search/:author', async (req,res) => { try { let results = await Critique.aggregate([ { $match: { author: req.params.author } }, { $lookup: { from: Review.collection.name, localField: 'reviewID', foreignField: 'id' as: 'reviews' }}, { $unwind: '$reviews' } ]); results = results.map( r => r.reviews ); console.log(results); res.json(results); } catch(e) { console.error(e); }});或者,如果没有,只需将所有id值传递给 $in :Or if you don't have that, then simply passing all id values to $in:app.get('/api/wherecritique/reviews/search/:author', async (req,res) => { try { let cris = await Critique.find({ author: req.params.author }); let results = await Review.find({ id: { $in: cris.map( c => c.reviewID ) } }); console.log(results); res.json(results); } catch(e) { console.error(e); }});根据您的MongoDB版本,这意味着对数据库的一个"或两个"实际调用.完全不需要循环异步调用.Which means either "one" or "two" actual calls to the database depending on your MongoDB version. No need to loop async calls at all.最后,确实不需要上述所有说明,而是正确使用 async.parallel 为:Finally, it's really not necessary as explained by all the above, but the correct usage of async.parallel would be:app.get('/api/wherecritique/reviews/search/:author', (req,res) => { Critique.find({author: req.params.author}, (err,cris) => { var tasks = cris.map( c => (callback) => Review.findOne({ id: c.reviewID }, callback); ); async.parallel( tasks, (err,results) => { if (err) throw err; // or something console.log(results); res.json(results); } ) });}); 这篇关于如何对多个查询使用异步并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 13:36