Filter data on the query or with Java stream? <p>I have a Play 2.3.x Java application connected to a MongoDB database via Morphia.</p> <p>I activated slow query profiling in MongoDB and saw that a query comes often. It looks like this :</p> <pre><code>"query": { "field1": null, "field2": true, "field3": "a061ee3f-c2be-477c-ad81-32f852b706b5", "$or": [ { "field4.VAL.VALUE_1": "EXPECTED_VALUE_1", "field4.VAL.VALUE_2": "EXPECTED_VALUE_2" } ] } </code></pre> <p>In its current state, there is no index so every time the query is executed the whole collection is scanned. I still have a few documents, but I anticipate the growth of the database.</p> <p>So I was wondering what was the best solution :</p> <ol> <li>Remove all the clauses above from the query, retrieve all results (paginated) and filter with Java <code>Stream API</code></li> <li>Keep the query as is and index the fields</li> </ol> <p>If you see another solution, feel free to suggest it :)</p> <p>Thanks.</p>