doctrine odm – how to generate a find query

I have a symfony 5 project, with mongodb as db and doctrine odm.

Everything looks and works fine, except that fact that on a huge collection every query takes ages. Indexes look fine.

After investigating this I noticed that using a query like the one below takes about 0.5 seconds (and that is ok considering data size and our needs):

db.getCollection('Logs').find({clientId: 1234567}).sort({'_id': 1});

Then I checked how doctrine generates the query, and it generates it like this, which is basically the same thing from an end result point of view:

db.getCollection('Logs').runCommand({"aggregate":"Logs","pipeline":[{"$match":{"clientId":1234567}},{"$sort":{"_id":1}}],"allowDiskUse":false,"cursor":{}})

The problem is that second variant takes about 115 seconds to execute.

Why is the second option so much slower (I’m guessing it doesn’t use indexes or something)?
Also… can I somehow "instruct" doctrine to use find instead of runCommand (project is built over https://api-platform.com/ and removing doctrine would require to give up on that and we kind of love it, so unless is absolutely necessary I would prefer to keep it)?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *