Back to top

ReQL command: coerce_to

Command syntax

sequence.coerce_to('array') → array

value.coerce_to('string') → string

string.coerce_to('number') → number

array.coerce_to('object') → object

sequence.coerce_to('object') → object

object.coerce_to('array') → array

binary.coerce_to('string') → string

string.coerce_to('binary') → binary


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 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 { |post|
    { :comments => r.table('comments').get_all(post['id'], {:index => 'post_id'}).coerce_to('array') }

Example: Coerce an array of pairs into an object.

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

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.


Get more help

Couldn't find what you were looking for?