mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Added support to fetch latest object from mongoengine queryset

Open ArpitSachan opened this issue 2 years ago • 4 comments

Solving: https://github.com/MongoEngine/mongoengine/issues/2756

ArpitSachan avatar May 28 '23 05:05 ArpitSachan

How come you are approving your own PRs?

gowthamvbhat avatar Jun 04 '23 07:06 gowthamvbhat

I'm not sure that it will work good in case of sort that already created on the queryset recommended to add tester to that case

idoshr avatar Sep 21 '23 12:09 idoshr

I'm not sure about the use case for such method but given that it's present in django, it's something that I'm ok to consider.

That being said, I see multiple issues with this proposal

  1. in case no order_by is present, existing first method will rely on no-sorting (i.e natural sorting). If last would rely on primary key sorting silently, it would mean that you could get inconsistencies (e.g get the same record) if you use first() and last().

  2. if an existing sorting is specified on the queryset, it should be re-used and not overwritten by the primary key ordering.

Fixing 2 can be easily done in this PR but if we want to tackle 1) correctly, we would have to add the same default ordering on first which I'm not necessarily keen to do...

What would be acceptable (and less disruptive) to do is the following:

  • add last but solely rely on the existing order_by (no letting it take any argument)
  • if no order_by is set on the queryset, raise an error to avoid any inconsistent behavior

bagerard avatar Dec 23 '23 23:12 bagerard