Reading documents

If you've been following along with the previous examples, your database will now have a table called fake_data that contains 100 documents. Now that we've got some data in our database, let's see how we can read data from RethinkDB from a Node.js script.

We've already seen how RethinkDB provides us with the filter command to read data from the database based on a condition. In the previous chapters, you learned to use this command in the web interface to perform read queries on our datasets.

However, sometimes you may want to read all the data from a table without any filtering. To do this, you just need to select the table from which you want to read, and RethinkDB will give you a cursor to access the data. You can then use the cursor to create an array that includes all the results.

This example reads and prints all the documents from the fake_data table:

r.table("fake_data").run(conn, function(err, cursor) {
    if (err) throw err;
    cursor.toArray(function(err, results) {
        if (err) throw err;
        console.log(results);
    });
});

Obviously, you first need to create a connection to the database and pass the connection as an argument to the run function. You can try running this command from the Node.js CLI; however, to limit the amount of data that gets printed to the screen, you can append the limit command at the end of the query:

r.table("fake_data").limit(5).run(conn, function(err, cursor) {

This will limit the number of retrieved documents to five. If you run this query from the Node.js CLI, you will receive an output similar to this:

[ 
  { email: '[email protected]',
    id: '00fc101b-aac6-4f2d-a354-ec6a887cf572',
    name: 'Muhammad Kuphal'
  },
  { email: '[email protected]',
    id: '01ad270a-8766-43d4-8473-72ce1bd47c08',
    name: 'Mr. Filiberto Larkin'
  },
  { email: '[email protected]',
id: '0005101a-e2e4-4959-affe-bdbb7356e41d',
name: 'Buford Bogan'
  },
  { email: '[email protected]',
id: '19ba2228-df99-46e8-8761-91793011a2b5',
name: 'Janessa Kutch'
  },
  { email: '[email protected]',
id: '1a2f817e-d1e2-4011-ace5-ede63a85085e',
name: 'Rhea Mills'
  }
]

As you can see from this example, reading data from RethinkDB using Node.js is extremely simple; however, more than often, you will want to do some kind of filtering on the data that you have in your database table.

As an example, suppose we want to read all documents that have a Gmail e-mail address. To do this, we can use the filter function to filter the data contained in our table, and we can use the match function to specify a regular expression, which will be used to filter results. In this example, the regular expression that we will be using is (gmail.com)$, which matches all the e-mails that end in gmail.com.

Now, let's look at the code. Create a new file called example2.js and type the following lines of code:

var r = require('rethinkdb');
r.connect({host: 'localhost', port: 28015}, function(err, conn) {
    if (err) throw err;
    r.table('fake_data').filter(function(doc) {
        return doc('email').match("(gmail.com)$")
    }).pluck('email').run(conn, function(err, cursor) {
        if (err) throw err;
        cursor.toArray(function(err, results) {
            if (err) throw err;
            console.log(results);
        });
    });
});

Let's take a closer look at how this code works:

  • First, we create a connection to the RethinkDB instance by calling the connect function and when the connection is established, we check for any errors.
  • Then, we chain the filter command, passing it a function as the argument. This function will be called on the data in the fake_data table, and the result of the function will establish if the data should be included in the results or not.
  • The filtering function uses the match command to match the e-mail field of each document using the specified regex pattern.
  • Finally, the pluck command is chained at the end of the query so that the results only include the e-mail field as this is the only field that we're interested in. As usual, the query is executed by calling the run command.

Similar to the previous example, this query provides us with a cursor. To obtain an array of results, we must call the toArray function on the cursor. When you've typed the lines of the script, save it as example2.js and run it by executing the following:

node example2.js
The resulting JSON document will be similar to this:
[ 
  { email: '[email protected]' },
  { email: '[email protected]' },
  { email: '[email protected]' },
  { email: '[email protected]' },
  { email: '[email protected]' }
]

As you can see, this simple script returns an array of documents containing the e-mails that end in gmail.com. By now, you should have learned how easy it is to read and write data to and from RethinkDB using Node.js. In the following section, we'll look at how to update and delete documents.

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

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