Skip to content

Instantly share code, notes, and snippets.

@coffeemug
Last active December 19, 2015 03:59
Show Gist options
  • Save coffeemug/5894446 to your computer and use it in GitHub Desktop.
Save coffeemug/5894446 to your computer and use it in GitHub Desktop.
Plucking nested objects in RethinkDB 1.7.

Syntax for plucking nested objects

In the previous versions of RethinkDB you could use the pluck command to extract only specific attributes from an object or a sequence of objects:

r.expr({a: 1, b: {c: 1, d: 1}}).pluck('b')
// returns `b: {c: 1, d: 1}`

r.expr([{a: 1, b: {c: 1, d: 1}},
        {a: 2, b: {c: 2, d: 2}}]).pluck('b')
// returns `[{b: {c: 1, d: 1}}, {b: {c: 2, d: 2}}]`

However, the pluck syntax wasn't powerful enough to extract nested attributes from the object (like c or d in the example above). The new pluck syntax introduces support for extracting nested attributes:

r.expr({a: 1, b: {c: 1, d: 1}}).pluck('b')
// returns `b: {c: 1, d: 1}`, like it did before

r.expr({a: 1, b: {c: 1, d: 1}}).pluck({b: {c: true}})
// returns `b: {c: 1}`

r.expr({a: 1, b: {c: 1, d: 1}}).pluck({b: {c: true, d: true}})
// returns `b: {c: 1, d: 1}`

// There is also a shortcut syntax to get multiple attributes on the
// same level of nestedness in the object
r.expr({a: 1, b: {c: 1, d: 1}}).pluck({b: ['c', 'd']})
// returns `b: {c: 1, d: 1}`

This syntax, of course works on sequences as well as objects. If the feedback on the pluck command is good, we'll include this syntax into other commands, like without, orderBy, etc.

See more about the 1.7 release in the release announcement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment