MongoDB user-defined function to find the factorial of a given number
kodingwindow@kw:~$ mongosh
...

test> function factorial(n){
... if(n<=-1)
... return 'Enter Positive Integer';
... if(n<=1)
... return 1;
... return n*factorial(n-1);
... }

test> factorial(0);
1

test> factorial(10);
3628800

test> factorial(-10);
Enter Positive Integer

test> factorial(170);
7.257415615307994e+306

test> factorial(171);
Infinity

test> quit()
kodingwindow@kw:~$ 
Advertisement