Back to top

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(post -> post.merge(
    r.hashMap("comments",
              r.table("comments").getAll(post.g("id")).optArg("index", "post_id")
              .coerceTo("array"))
)).run(conn);

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

r.expr(r.array(r.array("name", "Ironman"), r.array("victories", 2000)))
 .coerceTo("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.

r.expr(1).coerceTo("string").run(conn);

Get more help

Couldn't find what you were looking for?