Extremely slow loading of `payments` collection in Forest Admin (agent ^1.64.5, datasource-mongoose ^1.8.9)

Hello Forest Admin Support Team,
I’m encountering significant performance issues when accessing the payments collection in my Forest Admin panel. Loading times can reach up to 2 minutes, which makes the UI practically unusable.


Environment

"@forestadmin/agent": "^1.64.5",
"@forestadmin/datasource-mongoose": "^1.8.9",

Captured Aggregation Command

{
  "type": "command",
  "ns": "DB.payments"
  "command": {
    "aggregate": "payments",
    "pipeline": [
      { "$addFields": { /* dozens of nested fields flattened */ } },
      { "$addFields": { "riskEvaluation@@@decision": "$riskEvaluation.decision" } },
      { "$project": { /* exclude original nested paths */ } },
       {
        "$match": {
          "$and": [
            {
              "status": {
                "$eq": "REFUNDED"
              }
            },
            {
              "status": {
                "$ne": "EXPIRED"
              }
            },
            {
              "status": {
                "$ne": "OPEN"
              }
            },
            {
              "organizationId": {
                "$ne": "xxx"
              }
            },
            {
              "organizationId": {
                "$ne": "xxx"
              }
            },
            {
              "organizationId": {
                "$ne": "xxx"
              }
            },
            {
              "organizationId": {
                "$ne": "xxx"
              }
            },
            {
              "organizationId": {
                "$ne": "xxx"
              }
            },
            {
              "organizationId": {
                "$ne": "xxx"
              }
            }
          ]
        }
      },
      { "$sort": { "createdAt": -1 } },
      { "$skip": 0 },
      { "$limit": 100 },
      { "$project": { /* include only final fields */ } }
    ],
    "cursor": {},
    "$db": "DB"
  },
  "planSummary": "IXSCAN { status: -1 }",
  "durationMillis": 11255
  "keysExamined": 34095,
  "docsExamined": 34095,

}

Observed Metrics

  • keysExamined/docsExamined: ~34,000 each
  • Bytes read: ~2.5 GB
  • Duration: ~11 s per request, up to 2 min under load
  • Records returned: 100 (limit)

Questions & Requested Assistance

  1. Are there known issues in versions 1.64.5 / 1.8.9 that could generate suboptimal aggregation pipelines?
  2. Could there be a misconfiguration of flattenMode or datasource-mongoose defaults causing full collection scans?
  3. What diagnostic steps or logging should I enable to pinpoint pipeline inefficiencies?
  4. Is there any additional configuration or recommended index strategy to improve aggregate performance in Forest Admin?

Thank you for any guidance or troubleshooting steps you can provide. Please let me know if you need further details or logs.


Best regards,
Ilyas

Hi @ilyas :waving_hand: Indeed 2 minutes is a lot.
I have few questions,

  • Is the 2 calls (GET, COUNT) to your agent take the same time ?
  • I see the request and I suppose you access the collection by using a segment ? Is the issue the same without a segment configuration and any filters ?
  • What is your flatten configuration on the datasource creation ?
  • Do you have many relationship associated to the collection ?

Hi @Arnaud_Moncel :waving_hand:

Thanks for the follow-up. Please see my answers below:


1. GET vs COUNT timing

The count is disabled using collection.disableCount(), here’s what I observed in the logs:

Jul  3 09:49:00 ip-x.x.x.x web[595975]: #033[32minfo:#033[0m [200] GET /payments/count - 1ms
Jul  3 09:51:14 ip-x.x.x.x web[595975]: #033[32minfo:#033[0m [200] GET /payments - 224060ms

2. Segments / filters

The problem dissapears when accessing the payments with no segment and no scopes, but in our case we have to access it through segments.

some logs with and without segments/scopes

without:

Jul  3 16:11:58 ip-x.x.x.x web[595975]: #033[32minfo:#033[0m [200] GET /payments/count - 0ms
Jul  3 16:12:03 ip-x.x.x.x web[595975]: #033[32minfo:#033[0m [200] GET /payments - 46ms

with :

Jul  3 16:12:15 ip-x.x.x.x web[595975]: #033[32minfo:#033[0m [200] GET /payments/count - 1ms
Jul  3 16:14:07 ip-x.x.x.x web[595975]: #033[32minfo:#033[0m [200] GET /payments - 128501ms

3. Flatten configuration

In my forestadmin(...) bootstrap, I add each Mongoose datasource like this:

Object.entries(connections).forEach(([dbName, connection]) => {
  if (!connection) {
    console.error(`Connection ${dbName} is invalid.`);
    return;
  }

  console.log(`Adding datasource: ${dbName}`);
  agent.addDataSource(
    createMongooseDataSource(connection, {
      flattenMode: 'auto',
    })
  );
});

This flattenMode: 'auto' applies across all collections.


4. Relationships on payments


collection.addManyToOneRelation(
  "customer",
  "customers",
  { foreignKey: "c_id" }
);

// Removed inverse joins to payouts, refunds, and transactions:
collection.removeField(
  "payouts_through_payouts_paymentIds",
  "refunds_paymentId__manyToOne__inverse",
  "transactions_paymentId__manyToOne__inverse"
);

Only a single customer relation is being pulled in; all other inverse-join fields have been removed.


one other thing, when i execute the same aggregation in mongo compass i get the results fast.

aggregation i mean :

and when i use forest admin with no scope or segments its fast, but the second i introduce, even a single filter by status or date (which have indexing enabled) it takes a long time to get the results

Hi @Arnaud_Moncel and team,
Just checking in, I’m still waiting on feedback about the details I shared. Please let me know if you need anything else. Thanks!

Hi @ilyas, sorry i was on vacation since friday.
If i understand corretly, the issue disapear when you stop using any filter/segment/scope.
Did these filter using flattened field?

Have you extracted the request from forest or just “recreated” it? I’m trying to understand where does the slowness come from.

yes when i remove all scope filters, and use full collection not the segments, it works fast.
and yes i extracted the request that forest admin sent to mongo from mongo logs.

one other thing that might help, it used to work normal in
“forest-express-mongoose”: “^9.2.0”,
but when we updated to agent
@forestadmin/agent”: “^1.64.5”,
that’s when this problem appeared

Yes it is normal as the datasource work differently on the two version.

Ok so, if the request made by the agent run fast when you try to execute it directly in mongo, the issue come from between the call to mongo, and before the response to the UI.
Can you try to setup the flattenMode to legacy to see if something change?

It works good with legacy, could you explain why? and what is the difference between legacy and auto and why using auto gives this problem in my case?

Here there is the full explanation of what do each flattenMode.
I suppose your model is pretty complexe and have many nested object and use the flatten auto mode do a lot of thing under the hood.
Maybe, can you share with me your model schema so that I can try to reproduce it.