W3cubDocs

/RethinkDB JavaScript

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': '[email protected]', 'posts': [ 1, 4, 5 ] },
    { 'id': 2, 'user': 'george', 'email': '[email protected]' },
    { 'id': 3, 'user': 'jane', 'email': '[email protected]', 'posts': [ 2, 3, 6 ] }
]

Command and output:

> r.table('users').withFields('id', 'user', 'posts').run(conn, callback)
// 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', {contact: {phone: "work"}).run(conn, callback)

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/with_fields/