mongodb odm how to get highest value of a column?

非 Y 不嫁゛ 提交于 2019-12-14 04:01:03

问题


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:

  1. Find that specific collection
  2. Sort it with -1 the field you want
  3. Limit it to 1
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!