Collection:
[
{ _id: \"Foo\", flag1: false, flag2: true, flag3: false },
{ _id: \"Bar\", flag1: true, flag2: false, flag3: true }
]
The aggregate call can be passed a callback function, which is called after the aggregation has completed.
function getValuesAndMessges( params, callback ) {
db.collection.aggregate([
{ "$project": {
"_id": 1,
"flag1": { "$first": "$flag1" },
"flag2": { "$first": "$flag2" },
"flag3": { "$first": "$flag3" },
}}
], function( err, results ) {
if ( !err ) {
result.forEach( result => {
// process items in results here, setting a value
// using the actual logic for writing message ...
if( flag1 )
result.message = "broken";
else
result.messsge = 'OK';
});
}
callback(err, results);
});
}
this way each of your aggregated items (based on your conditions / parameters) will have a message property (or whatever properties you choose to write) set and you can use them in your calling function.