W3cubDocs

/RethinkDB JavaScript

ReQL command: coerceTo

Command syntax

sequence.coerceTo('array') → array
value.coerceTo('string') → string
string.coerceTo('number') → number
array.coerceTo('object') → object
sequence.coerceTo('object') → object
object.coerceTo('array') → array
binary.coerceTo('string') → string
string.coerceTo('binary') → binary

Description

Convert a value of one type into another.

  • a sequence, selection or object can be coerced to an array
  • a sequence, selection or an array of key-value pairs can be coerced to an object
  • a string can be coerced to a number
  • any datum (single value) can be coerced to to a string
  • a binary object can be coerced to a string and vice-versa

Example: Coerce a stream to an array to store its output in a field. (A stream cannot be stored in a field directly.)

r.table('posts').map(function (post) {
    return post.merge({ comments: r.table('comments').getAll(post('id'), {index: 'postId'}).coerceTo('array')});
}).run(conn, callback)

Example: Coerce an array of key-value pairs into an object.

r.expr([['name', 'Ironman'], ['victories', 2000]]).coerceTo('object').run(conn, callback)

Note: To coerce a list of key-value pairs like ['name', 'Ironman', 'victories', 2000] to an object, use the object command.

Example: Coerce a number to a string.

r.expr(1).coerceTo('string').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/coerce_to/