Slow query from mongodb log runs so fast on shell, slow when called from JAVA

Through a JAVA app , I have an aggregate pipeline which runs in , say 15 seconds , showing so in op_msg in mongodb log.
When I copy paste and run it on mongodb SHELL , it returns instantly .

The returned data is not much , and i do use toArray() on mongodb shell , just to make sure that the data is read as well .

Does the op_msg contain the amount of time it takes to send data over network ?

BOTH the JAVA app and the mongo shell are running on the same machine

Hi @Homer_Kommrad and welcome in the MongoDB Community :muscle: !

There is obviously something wrong. The execution time should be the same on both systems.
Would it be possible to share the Java code and the code you are using in the Mongo Shell to reproduce the same query?
Would it be possible to also share a few fake documents so we have a better idea of what is happening and also what indexes exist on this collection?

You could also try to use explain in Java and the Mongo Shell to see the differences and what is taking time.

Cheers,
Maxime.