问题
I wrote this, but i think it is wrong way.
$lastPointContent = $dm->createQueryBuilder('PointContent')
->sort('pageNumber','desc')->limit(1)->getQuery()->getSingleResult();
I can't find any other way to making it look better.
回答1:
I don't know exactly about the syntax for doctrine-odm or PHP regarding Mongo but it seems to me that what you must do is:
- Find that specific collection
- Sort it with -1 the field you want
- Limit it to 1
- Check if there if there is a 'next' element in the query and, if it has, get it - it's the max value!
Seems like you are perfoming that right as for mongo there's not a max() for a whole query as SQL has. max in MongoDB stands for limiting the query to an upper bound.
tl;dr: the approach you described is correct IMO.
Let me know if I stated something wrong 👌
来源:https://stackoverflow.com/questions/21659684/mongodb-odm-how-to-get-highest-value-of-a-column