MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Use a while loop in MongoDB Aggregation?

Is it possible to convert / use the following function in an aggregation pipeline?

int getFactor(int num1, int num2){
        int temp;
        while (num2 != 0){
            temp = num2;
            num2 = num1 % num2;
            num1 = temp;          
        }
        return num1;
    }

You can use it as javascript,with MongoDB 4.4

https://docs.mongodb.com/manual/reference/operator/aggregation/function/

Convert i don’t think you can.
Some languages like Clojure have (reduced …) to stop the reduce from being complete.
MongoQL doesn’t have loops or reduced option.