Query Exceeded Memory Limit NoDiskUseAllowed

I have shared mongodb M2 cluster and got this error while fetch data using query
“PlanExecutor error during aggregation :: caused by :: Sort exceeded memory limit of 33554432 bytes, but did not opt in to external sorting.”

But when I use same code but only DB is another then not getting error

And it is probably much smaller so sorting can be done without exceeding limit.

If the database is not smaller then most likely it has an index that support the sort. A sort using a supporting index simply traverse the index so it does not need as much memory.