W3cubDocs

/RethinkDB Java

ReQL command: hasFields

Command syntax

sequence.hasFields([selector1, selector2...]) → stream
array.hasFields([selector1, selector2...]) → array
object.hasFields([selector1, selector2...]) → boolean

Description

Test if an object has one or more fields. An object has a field if it has that key and the key has a non-null value. For instance, the object {'a':1,'b':2,'c':null} has the fields a and b.

When applied to a single object, hasFields returns true if the object has the fields and false if it does not. When applied to a sequence, it will return a new sequence (an array or stream) containing the elements that have the specified fields.

Example: Return the players who have won games.

r.table("players").hasFields("games_won").run(conn);

Example: Return the players who have not won games. To do this, use hasFields with not, wrapped with filter.

r.table("players").filter(
    row -> row.hasFields("games_won").not()
).run(conn);

Example: Test if a specific player has won any games.

r.table("players").get("b5ec9714-837e-400c-aa74-dbd35c9a7c4c")
 .hasFields("games_won").run(conn);

Nested Fields

hasFields lets you test for nested fields in objects. If the value of a field is itself a set of key/value pairs, you can test for the presence of specific keys.

Example: In the players table, the games_won field contains one or more fields for kinds of games won:

{
    "games_won": {
        "playoffs": 2,
        "championships": 1
    }
}

Return players who have the “championships” field.

r.table("players")
 .hasFields(r.hashMap("games_won", r.hashMap("championships", true)))
 .run(conn);

Note that true in the example above is testing for the existence of championships as a field, not testing to see if the value of the championships field is set to true. There’s a more convenient shorthand form available. (See pluck for more details on this.)

r.table("players").hasFields(r.hashMap("games_won", "championships")).run(conn);

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