I implemented a custom graphql resolver which does a series of pipelines inside aggregate
function. When I execute it , it takes about 20 seconds. I thought it was because of deployment of regional vs global but I changed it and it is still the same.
I tried running same thing in mongosh
which gave me result pretty fast with the instance of $cursor.
Then I changed my pipelines to only include search and match condition which works pretty fast. So I thought its because of the sorting and $lookup. I tried to keep only one and try which made the query slower. Finally I thought may be it is because of the database client that MongoDB Atlas provides from graphql is not native.
Here are the pipeline code that I tried.
const cluster = context.services.get("mongodb-atlas");
const items = cluster.db(DATABASE).collection(collection);
const pipeline = [
searchCondition, // for search index query
{ $match: conditions }, // for the default where clause conditions
...item_details_join, // contains $lookup
...item_extends_join, // contains $lookup
sorting, //default sorting by the attribute descending
{ $skip: skip },{ $limit: limit }, // pagination
]
.filter(
(f) => Object.keys(f).length > 0
);
console.log(JSON.stringify(pipeline,null))
return items.aggregate(pipeline).toArray();