FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

ContainsPath

This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics.

ContainsPath(path, in )
contains_path(path, in )
ContainsPath( path, in )
ContainsPath( path, in )
ContainsPath(path, in )

Description

The ContainsPath function returns true if the specified path exists within the result of the in expression, or false otherwise.

ContainsPath is useful when you need to distinguish between objects, arrays, or documents that contain a nested path and those that do not.

The path is an array containing path selectors. Each path selector is a number (representing an array offset) or a string (representing a field name). The path is evaluated from left to right, and each path selector after the first represents a nesting within the data structure of the result of the in expression.

For example, if you provide the path 0, a, 1, b, the result of the in expression has to be an array where the first item is an object with the field a, whose value is an array and the second item in that array is an object with the field b. The minimal structure that would satisfy that path is:

[
  {
    "a": [
      {},
      { "b": true }
    ]
  }
]

Parameters

Parameter Type Definition and Requirements

path

Array of Numbers and/or Strings

A path to a specified field or array entry within the in value.

in

Any

A value of any type.

Returns

A boolean value.

Examples

  1. Array offsets

    The following query returns true because the path 1, 2 (targeting the value c) exists within the provided array:

    client.query(
      q.ContainsPath(
        [1, 2],
        [1, ['a', 'b', 'c'], 3],
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    true
    result = client.query(
      q.contains_path(
        [1, 2],
        [1, ["a", "b", "c"], 3]
      )
    )
    print(result)
    True
    result, err := client.Query(
    	f.ContainsPath(
    		f.Arr{1, 2},
    		f.Arr{1, f.Arr{"a", "b", "c"}, 3} ))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    true
    try
    {
        Value result = await client.Query(
            ContainsPath(
                Path(1, 2),
                Arr(1, Arr("a", "b", "c"), 3)
            )
        );
    
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    BooleanV(True)
    ContainsPath(
      [1, 2],
      [1, ['a', 'b', 'c'], 3],
    )
    true
    Query metrics:
    •    bytesIn:  48

    •   bytesOut:  17

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 6ms

    •    retries:   0

  2. Field names

    The following query returns true because the path b, two exists because the provided object has a b field that contains an object with the two field:

    client.query(
      q.ContainsPath(
        ['b', 'two'],
        {
          a: 1,
          b: { two: 2 },
          c: 3,
        },
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    true
    result = client.query(
      q.contains_path(
        ["b", "two"],
        {
          "a": 1,
          "b": {"two": 2},
          "c": 3
        }
      )
    )
    print(result)
    True
    result, err := client.Query(
    	f.ContainsPath(
    		f.Arr{"b", "two"},
    		f.Obj{
    			"a": 1,
    			"b": f.Obj{"two": 2},
    			"c": 3,
    		}))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    true
    try
    {
        Value result = await client.Query(
            ContainsPath(
                Path("b", "two"),
                Obj(
                    "a", 1,
                    "b", Obj("two", 2),
                    "c", 3
                )
            )
        );
    
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    BooleanV(True)
    ContainsPath(
      ['b', 'two'],
      {
        a: 1,
        b: { two: 2 },
        c: 3,
      },
    )
    true
    Query metrics:
    •    bytesIn:  84

    •   bytesOut:  17

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 2ms

    •    retries:   0

  3. Mixing field names and array offsets

    The following query returns true because the path 2, first (which refers to the field containing Grace) exists in the provided array:

    client.query(
      q.ContainsPath(
        [2, 'first'],
        [
          { first: 'Alan', last: 'Perlis' },
          { first: 'Alan', last: 'Turing' },
          { first: 'Grace', last: 'Hopper' },
        ]
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    true
    result = client.query(
      q.contains_path(
        [2, "first"],
        [
          {"first": "Alan", "last": "Perlis"},
          {"first": "Alan", "last": "Turing"},
          {"first": "Grace", "last": "Hopper"},
        ]
      )
    )
    print(result)
    True
    result, err := client.Query(
    	f.ContainsPath(
    		f.Arr{2, "first"},
    		f.Arr{
    			f.Obj{"first": "Alan", "last": "Perlis"},
    			f.Obj{"first": "Alan", "last": "Turing"},
    			f.Obj{"first": "Grace", "last": "Hopper"},
    		}))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    true
    try
    {
        Value result = await client.Query(
            ContainsPath(
                Path(2, "first"),
                Arr(
                    Obj(),
                    Obj("first", "Alan", "last", "Perlis"),
                    Obj("first", "Alan", "last", "Turing"),
                    Obj("first", "Grace", "last", "Hopper")
                )
            )
        );
    
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    BooleanV(True)
    ContainsPath(
      [2, 'first'],
      [
        { first: 'Alan', last: 'Perlis' },
        { first: 'Alan', last: 'Turing' },
        { first: 'Grace', last: 'Hopper' },
      ]
    )
    true
    Query metrics:
    •    bytesIn: 169

    •   bytesOut:  17

    • 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!