This section looks at querying the database in detail. Querying returns a subset of documents in a table—from no documents at all to the entire table. Which documents get returned depends on what type of filtering we do in the query. The absence of the filter()
command matches everything in a table.
One of the most common queries that you might want to run is reading all documents from a table. While this may not be a very efficient query when the table contains thousands of entries as it requires scanning the entire table, it can definitely be useful for debugging purposes.
We can read the entire table from the database just by selecting the database and an appropriate table as follows:
r.db('test').table('people')
If you've been running the queries from the previous sections, the result will contain three documents:
We can use the filter()
command in the query to restrict our search. Filtering is as easy as specifying the value that we are looking for. For example, to find all the people that live in London, we can run the following query:
r.table('people').filter(r.row("address")("city").eq("London"))
This is a more complex query that deserves additional explanation. First we select the people
table, and then use the filter()
command to specify our search items. The filter command returns all the elements in the table that match the given predicate. In this case, we use the following syntax to choose the row that we want to examine:
r.row("address")("city")
This query block uses the ()
operator to tell RethinkDB that we want to check the city
attribute that is nested within the address
object. Finally, we use the eq()
command that simply tests if two values are equal.
Let's summarize what we just did: first we select the attribute we want to check, by using the row()
command and the ()
operator and we then verify if the value is equal to London
. If this predicate returns true
, RethinkDB adds the document to the result set; in fact, if we run this query from the Data Explorer we get the following output:
As you can see from the screenshot, the result only contains the documents of people who live in London.
Filtering can go beyond the exact matching that I've just described; in fact, it can match much more complex criteria, such as ranges. The ReQL query language provides us with a set of mathematical and logical operators that we can use for comparison filtering, such as the ge()
command that tests if a given value is greater than or equal to another.
Suppose we want to print just the documents related to people that were born in or after 1990, we can use the ge()
command for this purpose to test if the yearOfBirth
attribute in the documents is greater or equal to 1990. We can do this by running the following query:
r.table('people').filter(r.row("yearOfBirth").ge(1990))
As usual, first we select the people
table, then we use the filter()
command to specify the condition. In this case, we use r.row()
to choose the yearOfBirth
attribute and append the ge()
command to test the condition. As we can expect, the query result only contains documents of people born in or after 1990
:
There are some occasions in which you may not want all the key/value pairs in a document returned. If this is the case, we can use the pluck()
command to specify which keys should be included in the resulting document. Building on from the previous example, let's print just the e-mail address of all the people born after 1990
:
r.table('people').filter(r.row("yearOfBirth").ge(1990)).pluck("email")
By now you should be accustomed with the ReQL query language: the pluck()
command just gets chained to the end of the query. If we want, we can specify multiple keys to be returned, like so:
3.144.222.239