# Can you limit the number of returned records?

**URL:** <https://discuss.aerospike.com/t/can-you-limit-the-number-of-returned-records/1330>\
**Category:** General Discussion\
**Created:** [June 1, 2015, 8:03pm UTC](https://discuss.aerospike.com/t/can-you-limit-the-number-of-returned-records/1330 "2015-06-01T20:03:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casra](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@casra](https://discuss.aerospike.com/u/casra)\
**Post date:** [June 1, 2015, 8:03pm UTC](https://discuss.aerospike.com/t/can-you-limit-the-number-of-returned-records/1330/1 "2015-06-01T20:03:21Z")

</div>

I am using the python client library. When I have such statement:

query = client.query(‘test’, ‘raw\_data’)

How can I put a limit to the number of records returned ? My aim here is to create batches of data (depending on number of records returned) to handle my local threads, which are responsible to further process the data.

Is there such a thing as “.limit(100)” ?

Thank you.

---

<div class="post-metadata">

**Author:** ![rbotzer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/rbotzer/32/2114_2.png) [@rbotzer](https://discuss.aerospike.com/u/rbotzer)\
**Post date:** [June 2, 2015, 12:18am UTC](https://discuss.aerospike.com/t/can-you-limit-the-number-of-returned-records/1330/2 "2015-06-02T00:18:20Z")

</div>

A query in the Python client is initiated with either [`foreach()`](https://pythonhosted.org/aerospike/query.html#aerospike.Query.foreach) or [`results()`](https://pythonhosted.org/aerospike/query.html#aerospike.Query.results). You can use foreach to do the limit by having a counter that stops the query when it reaches the desired number.

For example:

```python
from __future__ import print_function
import aerospike
from aerospike import predicates as p

config = { 'hosts': [('192.168.119.3',3000)]}
client = aerospike.client(config).connect()

def limit(lim, result):
    c = [0]
    def key_add((key, metadata, bins)):
        if c[0] < lim:
            result.append(key)
            c[0] = c[0] + 1
        else:
            return False
    return key_add

query = client.query('test','aggr')
query.where(p.between('day', 1, 4))
keys = []
query.foreach(limit(100, keys))
print(len(keys))
client.close()

```

---

<div class="post-metadata">

**Author:** ![pratyyy](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@pratyyy](https://discuss.aerospike.com/u/pratyyy)\
**Post date:** [June 2, 2015, 5:50am UTC](https://discuss.aerospike.com/t/can-you-limit-the-number-of-returned-records/1330/3 "2015-06-02T05:50:17Z")

</div>

Hi casra,

Just to add to Ronen.

“Limit” feature in query is not yet implemented at the server side.

The queries in the example code above will be reported as failure at the server side. All query stats will be updated accordingly. You can think of this as a scenario where client is closing its connection after receiving N number of records. Since server is not aware of that it will treat this as a connection failure.

Thanks

---

<div class="post-metadata">

**Author:** ![leo\_sa](https://avatars.discourse-cdn.com/v4/letter/l/96bed5/32.png) [@leo\_sa](https://discuss.aerospike.com/u/leo_sa)\
**Post date:** [June 1, 2016, 7:25pm UTC](https://discuss.aerospike.com/t/can-you-limit-the-number-of-returned-records/1330/4 "2016-06-01T19:25:35Z")

</div>

Hey @pratyyy

Do you guys know when the limit feature would be implemented?

Thanks,
