mongodb odm how to get highest value of a column?

2019-08-04 05:28发布

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条回答
【Aperson】
2楼-- · 2019-08-04 06:06

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

查看更多
登录 后发表回答