W3cubDocs

/RethinkDB Python

ReQL command: with_fields

Command syntax

sequence.with_fields([selector1, selector2...]) → stream
array.with_fields([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 has_fields 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').with_fields('id', 'user', 'posts').run(conn)

[
    { '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').with_fields('id', 'user', {contact: {'phone': 'work'}).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/python/with_fields/