I want to perform a query that will return all the documents that contain a given string in ANY of their fields. For example, say I have a "users" table and I'm looking for all the documents that contain "john", the returned result can be:
[{"first_name": "jhon", "last_name": "watson"}, {"first_name": "elton", "last_name": "john"}, {"first_name": "sherlock", "last_name": "holmes", "best_friend": "john watson"}]
How do I do that in rethinkdb? javascript answer will do, python implementation will be better.
Unfortunately this query is made harder by the fact that ReQL doesn't have a
values
function like python. However it does have akeys
function so let's just use that to make avalues
function like so:Now that we have that finding a document that contains a string in one of its keys is pretty easy:
Finally we can put it all together:
You could in theory do this all in one big query but I really like breaking up even moderately complicated queries. The really nice thing about this approach is that if it doesn't work each function has a pretty simple set of semantics so you can debug them individually.