I have an advanced Mongo query.
Where I try to count distinct values for a number of fields.
First I have to iterate over three values, which are entries n my Mongoose schema, and then I have to count each distinct value for each of the respective field, and return them as a string.
const mappedStuff = this.featureFields.map(async field => {
return new Promise(async (resolve, reject) => {
const distinctValues = await this.gameModel.distinct(field);
return distinctValues.map(async entry => {
console.log(
`${field}, ${entry}, ${await this.gameModel.count({
[field]: entry,
})}`,
); //Logs out correct value
resolve(
`${field}, ${entry}, ${await this.gameModel.count({
[field]: entry,
})}` as string, //resolves instantly and does not return the correct value
);
});
});
});
console.log(Promise.all(mappedStuff));
return Promise.all(mappedStuff);
The console.log works fine, and I just want to return that value, I have tried pushing it to a list outside, but does not work, because I have an await
inside of the string.
Therefore I tried wrapping the entire thing in a promise, but this does not solve the issue either. Does anybody have a solution