Python cloudant query "_id" by regex

268 Views Asked by At

I am new to ibm cloudant and I am using the python API for cloudant for my web application. Is there any way I can retrieve documents from my couch database hosted on IBM cloudant instance using regex on "_id"? I've read the python-cloudant documentation but I couldn't find anything.

Please help. Thank you.

1

There are 1 best solutions below

2
On BEST ANSWER

If you consider the underlying API, you can use the $regex operator in Cloudant Query. However, it won't use an index, so performance will be pretty dire, as it will scan the whole database. If possible, try arrange your ids such that you can find the subset you need with a range query instead. Given a db that looks like so:

% curl https://skruger.cloudant.com/aaa/_all_docs
{"total_rows":4,"offset":0,"rows":[
{"id":"aaron","key":"aaron","value":{"rev":"1-..."}},
{"id":"adam","key":"adam","value":{"rev":"1-..."}},
{"id":"ben","key":"ben","value":{"rev":"1-..."}},
{"id":"charlie","key":"charlie","value":{"rev":"1-..."}}
]}

we can retrieve all docs with id starting with "a" only,

% curl 'https://skruger.cloudant.com/aaa/_all_docs?startkey="a"&endkey="b"'
{"total_rows":4,"offset":0,"rows":[
{"id":"aaron","key":"aaron","value":{"rev":"1-..."}},
{"id":"adam","key":"adam","value":{"rev":"1-..."}}
]}