@@ -98,6 +98,8 @@ class BaseQuery(object):
9898 The "order by" entries to use in the query.
9999 limit (Optional[int]):
100100 The maximum number of documents the query is allowed to return.
101+ limit_to_last (Optional[bool]):
102+ Denotes whether a provided limit is applied to the end of the result set.
101103 offset (Optional[int]):
102104 The number of results to skip.
103105 start_at (Optional[Tuple[dict, bool]]):
@@ -146,6 +148,7 @@ def __init__(
146148 field_filters = (),
147149 orders = (),
148150 limit = None ,
151+ limit_to_last = False ,
149152 offset = None ,
150153 start_at = None ,
151154 end_at = None ,
@@ -156,6 +159,7 @@ def __init__(
156159 self ._field_filters = field_filters
157160 self ._orders = orders
158161 self ._limit = limit
162+ self ._limit_to_last = limit_to_last
159163 self ._offset = offset
160164 self ._start_at = start_at
161165 self ._end_at = end_at
@@ -170,6 +174,7 @@ def __eq__(self, other):
170174 and self ._field_filters == other ._field_filters
171175 and self ._orders == other ._orders
172176 and self ._limit == other ._limit
177+ and self ._limit_to_last == other ._limit_to_last
173178 and self ._offset == other ._offset
174179 and self ._start_at == other ._start_at
175180 and self ._end_at == other ._end_at
@@ -224,6 +229,7 @@ def select(self, field_paths) -> "BaseQuery":
224229 field_filters = self ._field_filters ,
225230 orders = self ._orders ,
226231 limit = self ._limit ,
232+ limit_to_last = self ._limit_to_last ,
227233 offset = self ._offset ,
228234 start_at = self ._start_at ,
229235 end_at = self ._end_at ,
@@ -294,6 +300,7 @@ def where(self, field_path, op_string, value) -> "BaseQuery":
294300 orders = self ._orders ,
295301 limit = self ._limit ,
296302 offset = self ._offset ,
303+ limit_to_last = self ._limit_to_last ,
297304 start_at = self ._start_at ,
298305 end_at = self ._end_at ,
299306 all_descendants = self ._all_descendants ,
@@ -345,21 +352,51 @@ def order_by(self, field_path, direction=ASCENDING) -> "BaseQuery":
345352 field_filters = self ._field_filters ,
346353 orders = new_orders ,
347354 limit = self ._limit ,
355+ limit_to_last = self ._limit_to_last ,
348356 offset = self ._offset ,
349357 start_at = self ._start_at ,
350358 end_at = self ._end_at ,
351359 all_descendants = self ._all_descendants ,
352360 )
353361
354362 def limit (self , count ) -> "BaseQuery" :
355- """Limit a query to return a fixed number of results.
356-
357- If the current query already has a limit set, this will overwrite it.
363+ """Limit a query to return at most `count` matching results.
358364
365+ If the current query already has a `limit` set, this will override it.
366+ .. note::
367+ `limit` and `limit_to_last` are mutually exclusive.
368+ Setting `limit` will drop previously set `limit_to_last`.
359369 Args:
360370 count (int): Maximum number of documents to return that match
361371 the query.
372+ Returns:
373+ :class:`~google.cloud.firestore_v1.query.Query`:
374+ A limited query. Acts as a copy of the current query, modified
375+ with the newly added "limit" filter.
376+ """
377+ return self .__class__ (
378+ self ._parent ,
379+ projection = self ._projection ,
380+ field_filters = self ._field_filters ,
381+ orders = self ._orders ,
382+ limit = count ,
383+ limit_to_last = False ,
384+ offset = self ._offset ,
385+ start_at = self ._start_at ,
386+ end_at = self ._end_at ,
387+ all_descendants = self ._all_descendants ,
388+ )
362389
390+ def limit_to_last (self , count ):
391+ """Limit a query to return the last `count` matching results.
392+ If the current query already has a `limit_to_last`
393+ set, this will override it.
394+ .. note::
395+ `limit` and `limit_to_last` are mutually exclusive.
396+ Setting `limit_to_last` will drop previously set `limit`.
397+ Args:
398+ count (int): Maximum number of documents to return that match
399+ the query.
363400 Returns:
364401 :class:`~google.cloud.firestore_v1.query.Query`:
365402 A limited query. Acts as a copy of the current query, modified
@@ -371,6 +408,7 @@ def limit(self, count) -> "BaseQuery":
371408 field_filters = self ._field_filters ,
372409 orders = self ._orders ,
373410 limit = count ,
411+ limit_to_last = True ,
374412 offset = self ._offset ,
375413 start_at = self ._start_at ,
376414 end_at = self ._end_at ,
@@ -398,6 +436,7 @@ def offset(self, num_to_skip) -> "BaseQuery":
398436 field_filters = self ._field_filters ,
399437 orders = self ._orders ,
400438 limit = self ._limit ,
439+ limit_to_last = self ._limit_to_last ,
401440 offset = num_to_skip ,
402441 start_at = self ._start_at ,
403442 end_at = self ._end_at ,
0 commit comments