In Oracle rather than coding one query for each possible ORDER BY clause, you can specify a DECODE function that evaluates the user’s choice, and dynamically alters the ORDERY BY as following:
SELECT . . .
FROM emp
GROUP
BY DECODE(i_grouping_col,'E',emp_no,'D',dept_no);
Is there any way to practise decode function in MongoDB?
Yes there was db.collection.group() function which can be used to do what you are trying to do however it is depricated as of mongodb version 3.4 because of various limitations.
an example would be
db.orders.group(
{
key: { ord_dt: 1, 'item.sku': 1 },
cond: { ord_dt: { $gt: new Date( '01/01/2012' ) } },
reduce: function ( curr, result ) { },
initial: { }
}
)
the SQL equivalent is
SELECT ord_dt, item_sku
FROM orders
WHERE ord_dt > '01/01/2012'
GROUP BY ord_dt, item_sku
you can read more about it Here