sql >> Base de Datos >  >> NoSQL >> MongoDB

Cómo obtener un resultado múltiple en mangosta y combinarlo en una sola respuesta

Si está utilizando async-await luego intente como a continuación donde Promise .todo() te ayudará a ejecutar todas las operaciones en paralelo:

async function getCounts() {
    let [student,teacher,staff] = await Promise.all([student.countDocuments({}),teacher.countDocuments({}),staff.countDocuments({})]);
    return {student,teacher,staff};
}

/** call this function in main handler function where you get API call */
getCounts().then((data)=>{res.json(data)}).catch((err)=>{console.log(err)})