Why Atlas Functions are faster than just normal query?

Hi people,

I currently have an app registered with Atlas using the Realm SDK which will be obsolete in September this year, so I am transferring the data to an Ubuntu server that I am creating myself and will use JWT authentication, as I mentioned in another post.

However, when I created the services for this app in Atlas, in addition to storage and authentication, I used an Atlas Function to specify a particular aggregate.

exports = async function (dateInitial, dateFinal) {
const vendas = context.services.get(“mongodb-atlas”).db(“main”).collection(“vendas”);
const rows = context.user.custom_data.lojas.map(async (row) => {
const result = await vendas.aggregate([
{
$match: {
groupId: context.user.custom_data.groupId,
cnpj: row.base,
data: {
$gte: dateInitial,
$lte: dateFinal
}

            }
        },
        {
            $facet: {
                total: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$total" },
                            count: { $sum: 1 }
                        }
                    }
                ],
                p1: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p1" }
                        }
                    }
                ],
                p2: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p2" }
                        }
                    }
                ],
                p3: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p3" }
                        }
                    }
                ],
                p4: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p4" }
                        }
                    }
                ],
                p5: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p5" }
                        }
                    }
                ],
                p6: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p6" }
                        }
                    }
                ],
                p7: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p7" }
                        }
                    }
                ],
                p8: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$p8" }
                        }
                    }
                ],
                troco: [
                    {
                        $group: {
                            _id: null,
                            valor: { $sum: "$troco" }
                        }
                    }
                ]
            }
        }
    ]).toArray();

    return result[0];
});

return Promise.all(rows);

};

And I only did this because I discovered that this aggregate was 10 times faster when I used the RealmSDK to call this Atlas Function, instead of executing the aggregate directly through the Realm SDK, but there is a detail… It only became fast when I specified the System user in Role → Settings → Authentication → System.

Why does this increase in speed happen? How to reproduce this in my MongoDB on Ubuntu?

Is it possible that you’re creating a net new connection to your Atlas cluster on every query? (that would involve a TLS and SCRAM auth handshake time-cost overhead that you can avoid with an opened connection pool that your application can re-use)

Hi @Andrew_Davidson

I don’t now…

First I used this command, the return was slow:

const app = new Realm.App({ id: ID_APP })
const mongo = app.currentUser.mongoClient("mongodb-atlas").db(app.currentUser.customData.lojas[i].base)
const data = await mongo.collection("vendas").aggregate(...);

Then, as I said, I created an Atlas Function to execute this aggregate and started calling it directly, as shown below, which improved the speed 10 times:

const app = new Realm.App({ id: ID_APP })
const data = await app.currentUser.functions["getStatistic1"]();

And now that I’m migrating the data and I’m no longer using the Atlas Functions, basically in the REST API every time the user accesses the endpoint, for example, https:my_domain/statistics the aggregate is executed and returns the data to it, however with the same slowness as in the first case.

In this scenario that I reported, does the Atlas Function use this pool?

How can I use this?

Thx for the help