Utilizing mongodb aggregation
with the following sample data:
{
"name": "John wire",
"city": "New York"
},
{
"name": "mike jansen",
"city": "Dubai"
}
...etc
and my aggregation code for returning is as follows:
], function (err, result) {
if (err) {
logger.error(req.method + ": " + req.originalUrl + ", message: " + err.message)
next(createError.InternalServerError())
}
res.send(result); //this line
});
In addition, I have a function that capitalizes every first word of the field name
:
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}
Example: John wire -> John Wire
How can I format the data in result.name
before it is returned?
Your input would be greatly appreciated. Thank you.