0

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 Answer 1

3

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-..."}}
]}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for replying. My _id is a string, I wonder if range could be used in this case.
I've edited my answer to show a string range query example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.