Back to top

    ReQL command: filter

    Command syntax

    selection.filter(predicate_function) → selection

    stream.filter(predicate_function) → stream

    array.filter(predicate_function) → 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 optArg.

    • If default is set to true, documents with missing fields will be returned rather than skipped.
    • If default is set to r.error(), a 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(r.hashMap(age, 30)).run(conn);
    

    The predicate r.hashMap(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 r.hashMap(field, value) style of predicate is useful for exact matches, a more general way to write a predicate is to use an anonymous function that returns true or false.

    r.table("users").filter(row -> row.g("age").eq(30)).run(conn);
    

    In this case, the function returns true if the field age is equal to 30.

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

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

    r.table("users").filter(row -> row.g("age").gt(18)).run(conn);
    

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

    r.table("users").filter(
        row -> row.g("age").lt(18).and(row.g("age").gt(13))
    ).run(conn);
    

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

    r.table("users").filter(
        row -> row.g("age").ge(18).or(row.g("hasParentalConsent"))
    ).run(conn);
    

    More complex predicates

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

    r.table("users").filter(
        user -> user.g("subscription_date").during(
            r.time(2012, 1, 1, "Z"), r.time(2013, 1, 1, "Z"))
    ).run(conn);
    

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

    r.table("users").filter(
        user -> user.g("email").match("@gmail.com$")
    ).run(conn);
    

    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(
        user -> user.g("placesVisited").contains("France")
    ).run(conn);
    

    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(
        r.hashMap("name", r.hashMap("first", "William").with("last", "Adama"))
    ).run(conn);
    

    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(
        r.hashMap("name", r.hashMap("first", "William").with("last", "Adama"))
    )).run(conn);
    

    You may rewrite these with anonymous functions.

    r.table("users").filter(
        user -> user.g("name").g("first").eq("William")
            .and(user.g("name").g("last").eq("Adama"))
    ).run(conn);
    
    r.table("users").filter(
        user -> user.g("name")
            .eq(r.hashMap("first", "William").with("last", "Adama"))
    ).run(conn);
    

    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(row -> row.g("age").lt(18)).optArg("default", true).run(conn);
    

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

    r.table("users").filter(row -> row.g("age").gt(18))
     .optArg("default", r.error()).run(conn);
    

    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(user -> user.hasFields("phone_number")).run(conn);
    

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

    r.table("users").filter(
        user -> user.g("role").eq("editor").default_(false)
            .or(user.g("privilege").eq("admin").default_(false))
    ).run(conn);
    

    Instead of using a default optArg with 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.

    Get more help

    Couldn't find what you were looking for?