0

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;
2

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.