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 value is returned. If the path does not exist and the default value is not specified, an error is returned.

Paths

The path parameter defines which part of from should be selected and returned.

  • If from is an Array, path can be expressed as a Long which is interpreted a the zero-based index from the start of the array. For example:

    Select(2, ['one', 'two', 'three', 'four'])
    'three'
  • If from is an Object, path can be expressed as a String, which is interpreted as the name of a field within from. For example:

    Select('b', {a: 'one', b: 'two', c: 'three'})
    'two'
  • If from is an Object that contains nested objects, path can be expressed as an Array containing field names, or array offsets, that lead to the desired value within the structure of from. For example:

    Let(
      {
        from: {
          timestamp: Now(),
          leaders: [
            {
              player: 'eskwayrd',
              position: [53, 47],
              score: 11,
            },
            {
              player: 'databrecht',
              position: [11, 22],
              score: 10,
            },
            {
              player: 'n400',
              position: [37, 4],
              score: 7,
            },
          ],
        }
      },
      Select(['leaders', 1, 'position', 1], Var('from'))
    )
    22

Parameter

Parameter Type Definition and Requirements

path

The field name path (the list of field names or array offsets required to access a specific field nested within the document structure), array offset, or field name within from to select.

from

Array or Object

The value containing the data to be selected.

default

Any

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

Returns

The value at the path within from, or the default value if the path does not exist within `from.

Examples

  1. 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] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    munchings
    result = client.query(
      q.select(
        ["favorites", "foods", 1],
        {
          "favorites": {"foods": ["crunchings", "munchings", "lunchings"]}
        }
      )
    )
    print(result)
    munchings
    Select(
      ['favorites', 'foods', 1],
      { favorites: { foods: ['crunchings', 'munchings', 'lunchings'] } },
    )
    'munchings'
    Query metrics:
    •    bytesIn: 126

    •   bytesOut:  24

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 5ms

    •    retries:   0

  2. The following query uses Select to extract the document ID 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("prydain"))
        ).get());
    "prydain"
    client.query(
      q.Select(['id'], q.Database('prydain'))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    prydain
    result = client.query(
      q.select(["id"], q.database("prydain"))
    )
    print(result)
    prydain
    Select(['id'], Database('prydain'))
    'prydain'
    Query metrics:
    •    bytesIn:  47

    •   bytesOut:  22

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:  62

    • writeBytes:   0

    •  queryTime: 6ms

    •    retries:   0

  3. The following query demonstrates how the default value is returned when the path is not found within from:

    try
    {
        Value result = await client.Query(
            Select("e", Obj("a", 1, "b", 2), "The default")
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    StringV(The default)
    The Go version of this example is not currently available.
    System.out.println(
        client.query(
            Select(
                Arr(Value("e")),
                Obj("a", Value(1), "b", Value(2)),
                Value("The default")
            )
        ).get());
    "The default"
    client.query(
      q.Select('e', { a: 1, b: 2 }, 'The default')
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    The default
    result = client.query(
      q.select("e", {"a": 1, "b": 2}, "The default")
    )
    print(result)
    The default
    Select('e', { a: 1, b: 2 }, 'The default')
    'The default'
    Query metrics:
    •    bytesIn:  70

    •   bytesOut:  26

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 2ms

    •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!