Introducing ReQL

A database is only as good as its query language.

For a developer, the query language is basically an interface to the database; however, it's disappointing to say that most of the existing query languages are easy to use or powerful but not both. Thankfully, with ReQL, you get the best of both worlds as the query language is intuitive but very powerful at the same time.

An explicit query language

Instead of writing complex, SQL-like queries, ReQL lets you write practical, simple queries. One of its best features is that ReQL can be considered an explicit query language. The way in which you approach RethinkDB is not only simple and intuitive, but it also gives you a good idea of how the query is being executed within the database, giving you the right balance of abstraction.

Let's look at an example query:

r.table('users').filter({ name: 'Alex' }).orderBy(r.desc('age'))

In this simple query, we get all the users with the name Alex ordered in descending order by age. If we were to run this query in a traditional SQL-like language, we would have no idea of how the query is being executed. Is the filtering being executed before or after the order by? We have no way to know this. However, by looking at the ReQL query, we immediately understand the query flow. First, the database accesses the users table, then it filters the results based on the name attribute, and finally, it orders by name. Being an explicit language, ReQL gives a developer the ability to understand exactly how the query is being executed.

As you have seen from the previous example, all ReQL queries are chainable. You start a query with the r identifier, and incrementally, you add transformers at the end of the query using the "." operator. In the following section, we will go over how to construct a basic query.

Building a query

When we're assembling a ReQL query the first thing we need to do is to start with the r identifier. The next step is accessing the correct database. Suppose we have a database called test and a table called users, we can select the database with the following transformer:

db("test")

The following step is to view to our table. We can select it by chaining the table() transformer to the first part of the query and separating each query block using the "." operator. Our query becomes as follows:

r.db("test").table("users")

This is actually a working query! If you run it from the Data Explorer section of RethinkDB's web interface, the query will print all the documents contained in the users table if it is present in the database.

The following steps in building a query depend on how you want to interact with the database. If we want to add some data to the table, you just need to append the insert() command to the initial query. On the other hand, if we want to query the table and filter the results, we will use the filter() command.

Here is an example that uses filter to print all the documents contained in the table where the city attribute is equal to London:

r.db("test").table("users").filter(r.row("address")("city").eq("London"))

Don't worry if you don't fully understand the previous query. We will have an in-depth look at these commands in the following sections. For now, it's important for you to learn that ReQL queries are built by chaining individual commands one after the other.

Now that you've learned how to assemble a query, let's dive deep into the database and start inserting data into RethinkDB!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.90.246