Select

Select( path, from, default )
Select( path, from, default )
Select( path, from, default )
Select( path, from, default )
select( path, from, default )
Select( path, from, default )

Description

The Select function extracts a single value from a document. It extracts the value specified by the path parameter out of the from parameter and returns the value. If the path does not exist, the optional default object is returned. If the default object is not provided, an error is returned.

Parameter

Argument Type Definition and Requirements

path

Long or String

The path to a field in the document to extract.

from

Object

The object containing the data to be extracted.

default

Object

Optional - The value to be returned if the path does not exist.

Returns

The value at the path.

Examples

The following query extracts from the top level object named "favorites" and second level array called "foods" the value in position 1 of the array. This value is "munchings".

try
{
    Value result = await client.Query(
        Select(
            Arr("favorites", "foods", 1),
            Obj(
                "favorites", Obj("foods", Arr("crunchings", "munchings", "lunchings"))
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
StringV(munchings)
result, err := client.Query(
	f.Select(
		f.Arr{"favorites", "foods", 1},
		f.Obj{
			"favorites": f.Obj{
				"foods": f.Arr{"crunchings", "munchings", "lunchings"}}}))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
munchings
System.out.println(
    client.query(
        Select(
            Arr(Value("favorites"), Value("foods"), Value(1)),
            Obj(
                "favorites", Obj(
                    "foods", Arr(
                        Value("crunchings"),
                        Value("munchings"),
                        Value("lunchings")
                    )
                )
            )
        )
    ).get());
"munchings"
client.query(
  q.Select(
    ['favorites', 'foods', 1],
    { favorites: { foods: ['crunchings', 'munchings', 'lunchings'] } },
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
munchings
result = client.query(
  q.select(
    ["favorites", "foods", 1],
    {
      "favorites": {"foods": ["crunchings", "munchings", "lunchings"]}
    }
  )
)
print(result)
munchings
try {
  println(Await.result(
    client.query(
  Select(
    Arr("favorites", "foods", 1),
    Obj(
      "favorites" -> Obj("foods" -> Arr("crunchings", "munchings", "lunchings"))
    ))),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
"munchings"

The following example uses Select to extract the "id" attribute from the Reference.

try
{
    Value result = await client.Query(
        Select(Arr("id"), Database("prydain"))
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
StringV(prydain)
result, err := client.Query(
	f.Select(f.Arr{"id"}, f.Database("prydain")))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
prydain
System.out.println(
    client.query(
        Select(Arr(Value("id")), Database(Value("prydain")))
    ).get());
"prydain"
client.query(
  q.Select(['id'], q.Database('prydain'))
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
prydain
result = client.query(
  q.select(["id"], q.database("prydain"))
)
print(result)
prydain
try {
  println(Await.result(
    client.query(Select(Arr("id"), Database("prydain"))),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
"prydain"

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!