I'm using nodejs with mongoose library for my mongoDB interaction.
I'm building a landing page query (think Netflix page) and need to get ~15 results for each categories (think Comedy, Horror, Drama, ...).
It seems not like a great solution to send a query for each "category" and deal with the callback issues as well as a heavy request on the database for a simple page load...as illustrated below:
const categories = ['horror','drama','comedy']
let counterCallback = 0
let allShows = []
categories.map(category =>{
Show.find({category: category},{limit: 15}, (err, shows)=>{
if(shows){ allShows.push(shows)}
allShows++
if(allShows === categories.length){ return res.json(allShows) }
})
})
I was thinking maybe of making an $or request, but I can't limit for the number for each "or"s ...
const categories = ['horror','drama','comedy']
Show.find({
$or:categories.map(category=> {return {category: category} })
},{limit: 10})
How can I achieve a request that limits the number of results per "category"?
Thanks