How exactly would one use the MongoDB aggregate function to get the same result as the following MySQL query?
sqlite> SELECT company, COUNT(*), MAX(salary) FROM Employees GROUP BY company;
How exactly would one use the MongoDB aggregate function to get the same result as the following MySQL query?
sqlite> SELECT company, COUNT(*), MAX(salary) FROM Employees GROUP BY company;
You can try something like
db.collection.aggregate([
{ $group: { _id: "$company", count: { $sum: 1 }, max: { $max: "$salary" } } },
{ $project: { _id: 0, company: "$_id", count: 1, max: 1 } },
]);
$project stage is added at the end of the pipeline for just renaming the _id field to company in the final output