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?