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?