Connection to atlas cluster is very slow and the simple find query with 900 records takes 30-40 seconds

Mongodb atlas cluster and nodejs configuration we are using:

Cluster: M10
RAM: 2GB
Mongodb version: 4.4
Mongoose version: ^6.1.5
Nodejs version: 12.x

We are using Serverless architecture in our codebase along with cloud provider AWS. Our aggregation query is with a simple match condition on user id to fetch data for a particular user, but the query takes around 30-40 seconds to return the documents. This behaviour was not observed with lesser number of documents, it occurred only when documents increased in number.

This is the aggregation query -

{
  $match: {
     user_id: 1
  }
}

We have also tried querying the same data with simple find, but the issue still persists.
Find query is -

{
user_id: 1
}

When the same query is run on Atlas explain plan it is executed within 1-2 ms while the same takes seconds to execute when the cluster is connected from local server.
For making the query fast, we have even added the index on the user id and also added hint of the index with query. Also added limit and offset to fetch data in batches, but nothing seems to change the response time of the query.

Document states are as given below:
Total number of documents: 1925
Documents that are examined and returned: 975
Size of the collection being queried: 18.3 MB

What could be the issue here? Is it the RAM size that is not sufficient to handle the data and we need to upgrade the cluster or we are missing out on something very basic?

1 Like

Hi @Avani_Khabiya,

Thanks for providing the details on the aggregation and the environment.

To try further assist here, can you provide the following information:

  1. The output from db.collection.explain("allPlansExecution").aggregate(...)
  2. The output from db.collection.getIndexes()

We are using Serverless architecture in our codebase along with cloud provider AWS

Could you further describe your architecture being used here? I presume this would be Lambda but please clarify in as much detail as possible.

This behaviour was not observed with lesser number of documents, it occurred only when documents increased in number.

Can you provide any examples (including code)

When the same query is run on Atlas explain plan it is executed within 1-2 ms while the same takes seconds to execute when the cluster is connected from local server.

Just to clarify here, do you mean that when connecting from a non-serverless environment to the same cluster and running the same aggregation, the time taken to complete the aggregation operation is significantly less than the 30-40 seconds experienced on the serverless environment?

With all of the above requested information, please redact any personal or sensitive information.

Regards,
Jason