Back to top

ReQL command: withFields

Command syntax

sequence.withFields([selector1, selector2...]) → stream

array.withFields([selector1, selector2...]) → array

Description

Plucks one or more attributes from a sequence of objects, filtering out any objects in the sequence that do not have the specified fields. Functionally, this is identical to hasFields followed by pluck on a sequence.

Example: Get a list of users and their posts, excluding any users who have not made any posts.

Existing table structure:

[
    { "id": 1, "user": "bob", "email": "bob@foo.com", "posts": [ 1, 4, 5 ] },
    { "id": 2, "user": "george", "email": "george@foo.com" },
    { "id": 3, "user": "jane", "email": "jane@foo.com", "posts": [ 2, 3, 6 ] }
]

Command and output:

r.table("users").withFields("id", "user", "posts").run(conn);

// Result passed to callback
[
    { "id": 1, "user": "bob", "posts": [ 1, 4, 5 ] },
    { "id": 3, "user": "jane", "posts": [ 2, 3, 6 ] }
]

Example: Use the nested field syntax to get a list of users with cell phone numbers in their contacts.

r.table("users").withFields("id", "user",
    r.hashMap("contact", r.hashMap("phone", "work"))
).run(conn);

Get more help

Couldn't find what you were looking for?