W3cubDocs

/RethinkDB JavaScript

ReQL command: filter

Command syntax

selection.filter(predicate_function[, {default: false}]) → selection
stream.filter(predicate_function[, {default: false}]) → stream
array.filter(predicate_function[, {default: false}]) → array

Description

Return all the elements in a sequence for which the given predicate is true. The return value of filter will be the same as the input (sequence, stream, or array). Documents can be filtered in a variety of ways—ranges, nested values, boolean conditions, and the results of anonymous functions.

By default, filter will silently skip documents with missing fields: if the predicate tries to access a field that doesn’t exist (for instance, the predicate {age:30} applied to a document with no age field), that document will not be returned in the result set, and no error will be generated. This behavior can be changed with the default optional argument.

  • If default is set to true, documents with missing fields will be returned rather than skipped.
  • If default is set to r.error(), an ReqlRuntimeError will be thrown when a document with a missing field is tested.
  • If default is set to false (the default), documents with missing fields will be skipped.

Note: filter does not use secondary indexes. For retrieving documents via secondary indexes, consider getAll, between and eqJoin.

Basic predicates

Example: Get all users who are 30 years old.

r.table('users').filter({age: 30}).run(conn, callback);

The predicate {age:30} selects documents in the users table with an age field whose value is 30. Documents with an age field set to any other value or with no age field present are skipped.

While the {field:value} style of predicate is useful for exact matches, a more general way to write a predicate is to use the row command with a comparison operator such as eq or gt, or to use an anonymous function that returns true or false.

r.table('users').filter(r.row("age").eq(30)).run(conn, callback);

In this case, the predicate r.row("age").eq(30) returns true if the field age is equal to 30. You can write this predicate as an anonymous function instead:

r.table('users').filter(function (user) {
    return user("age").eq(30);
}).run(conn, callback);

Predicates to filter are evaluated on the server, and must use ReQL expressions. You cannot use standard JavaScript comparison operators such as ==, </> and ||/&&.

Also, predicates must evaluate document fields. They cannot evaluate secondary indexes.

Example: Get all users who are more than 18 years old.

r.table("users").filter(r.row("age").gt(18)).run(conn, callback)

Example: Get all users who are less than 18 years old and more than 13 years old.

r.table("users").filter(
    r.row("age").lt(18).and(r.row("age").gt(13))
).run(conn, callback);

Example: Get all users who are more than 18 years old or have their parental consent.

r.table("users").filter(
    r.row("age").ge(18).or(r.row("hasParentalConsent"))
).run(conn, callback);

More complex predicates

Example: Retrieve all users who subscribed between January 1st, 2012 (included) and January 1st, 2013 (excluded).

r.table("users").filter(function (user) {
    return user("subscriptionDate").during(
        r.time(2012, 1, 1, 'Z'), r.time(2013, 1, 1, 'Z'));
}).run(conn, callback);

Example: Retrieve all users who have a gmail account (whose field email ends with @gmail.com).

r.table("users").filter(function (user) {
    return user("email").match("@gmail.com$");
}).run(conn, callback);

Example: Filter based on the presence of a value in an array.

Given this schema for the users table:

{
    name: String
    placesVisited: [String]
}

Retrieve all users whose field placesVisited contains France.

r.table("users").filter(function(user) {
    return user("placesVisited").contains("France")
}).run( conn, callback)

Example: Filter based on nested fields.

Given this schema for the users table:

{
    id: String
    name: {
        first: String,
        middle: String,
        last: String
    }
}

Retrieve all users named “William Adama” (first name “William”, last name “Adama”), with any middle name.

r.table("users").filter({
    name: {
        first: "William",
        last: "Adama"
    }
}).run(conn, callback)

If you want an exact match for a field that is an object, you will have to use r.literal.

Retrieve all users named “William Adama” (first name “William”, last name “Adama”), and who do not have a middle name.

r.table("users").filter(r.literal({
    name: {
        first: "William",
        last: "Adama"
    }
})).run(conn, callback)

You may rewrite these with anonymous functions.

r.table("users").filter(function(user) {
    return user("name")("first").eq("William")
        .and(user("name")("last").eq("Adama"));
}).run(conn, callback);

r.table("users").filter(function(user) {
    return user("name").eq({
        first: "William",
        last: "Adama"
    });
}).run(conn, callback);

Handling missing fields

By default, documents missing fields tested by the filter predicate are skipped. In the previous examples, users without an age field are not returned. By passing the optional default argument to filter, you can change this behavior.

Example: Get all users less than 18 years old or whose age field is missing.

r.table("users").filter(
    r.row("age").lt(18), {default: true}
).run(conn, callback);

Example: Get all users more than 18 years old. Throw an error if a document is missing the field age.

r.table("users").filter(
    r.row("age").gt(18), {default: r.error()}
).run(conn, callback);

Example: Get all users who have given their phone number (all the documents whose field phoneNumber exists and is not null).

r.table('users').filter(function (user) {
    return user.hasFields('phoneNumber');
}).run(conn, callback);

Example: Get all users with an “editor” role or an “admin” privilege.

r.table('users').filter(function (user) {
    return (user('role').eq('editor').default(false).
        or(user('privilege').eq('admin').default(false)));
}).run(conn, callback);

Instead of using the default optional argument to filter, we have to use default values on the fields within the or clause. Why? If the field on the left side of the or clause is missing from a document—in this case, if the user doesn’t have a role field—the predicate will generate an error, and will return false (or the value the default argument is set to) without evaluating the right side of the or. By using .default(false) on the fields, each side of the or will evaluate to either the field’s value or false if the field doesn’t exist.

Related commands

Get more help

Couldn't find what you were looking for?

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/javascript/filter/