If I have a controller like this:
const Review = require('../models/review')
const Launch = require('../models/launch')
async function getReviewsByUserId (req, res) {
const { userId } = req.params
const reviews = await Review.find({ userId }).lean() || []
return res.status(200).send(reviews.reverse())
}
How can I use the returned reviews to query more objects in the fastest way possible? Example of what I mean:
async function getReviewsByUserId (req, res) {
const { userId } = req.params
const reviews = await Review.find({ userId }).lean() || []
let launches = []
for (review in reviews) {
let launch = await Launch.find({launchId: review.launchId})
launches.push(launch)
}
return res.status(200).send(launches.reverse())
}
So as you can see, I simply loop though all my reviews and then use the review.launchId to lookup launches and push each to an array and return that.
Is this efficient? Is there a faster way to accomplish the same thing?
find()s into a singleaggregate()that uses$lookup.