Have a collection (sells by branches) called "operations" with documents like:
{id: 1, branch: "a", amount: 5, type: "sell"}
{id: 2, branch: "a", amount: 9, type: "pass"}
{id: 3, branch: "a", amount: 12, type: "credit"}
{id: 4, branch: "b", amount: 2, type: "pass"}
{id: 5, branch: "b", amount: 22, type: "pass"}
{id: 6, branch: "b", amount: 6, type: "sell"}
We need to get field type where amount is max
GROUP BY branch.
So, as result we might have
{branch: "a", type: "credit"}
{branch: "b", type: "pass"}