W3cubDocs

/RethinkDB Java

ReQL command: default

Command syntax

value.default(default_value) → any
sequence.default(default_value) → any

Description

Provide a default value in case of non-existence errors. The default command evaluates its first argument (the value it’s chained to). If that argument returns null or a non-existence error is thrown in evaluation, then default returns its second argument. The second argument is usually a default value, but it can be a function that returns a value.

Example: Suppose we want to retrieve the titles and authors of the table posts. In the case where the author field is missing or null, we want to retrieve the string Anonymous.

r.table("posts").map(post ->
    r.hashMap("title", post.g("title"))
        .with("author", post.g("author").default_("Anonymous"))
).run(conn);

We can rewrite the previous query with r.branch too.

r.table("posts").map(post ->
    r.branch(
        post.hasFields("author"),
        r.hashMap("title", post.g("title"))
            .with("author", post.g("author")),
        r.hashMap("title", post.g("title"))
            .with("author", "Anonymous")
    )
).run(conn);

Example: The default command can also be used to filter documents. Suppose we want to retrieve all our users who are not grown-ups or whose age is unknown (i.e., the field age is missing or equals null). We can do it with this query:

r.table("users").filter(
    user -> user.g("age").lt(18).default_(true)
).run(conn);

One more way to write the previous query is to set the age to be -1 when the field is missing.

r.table("users").filter(
    user -> user.g("age").default_(-1).lt(18)
).run(conn);

Another way to do the same query is to use hasFields.

r.table("users").filter(
    user -> user.hasFields("age").not().or(user.g("age").lt(18))
).run(conn);

The body of every filter is wrapped in an implicit .default_(false). You can overwrite the value false with the default optArg to filter, so the previous query can also be written like this.

r.table("users").filter(
    user -> user.g("age").lt(18).default_(true)
).optArg("default", true).run(conn);

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/java/default/