ContainsPath

ContainsPath( path, in )
ContainsPath( path, in )
ContainsPath( path, in )
ContainsPath(path, in )
contains_path(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

Argument 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:

    Value result = await client.Query(
        ContainsPath(
            Path(1, 2),
            Arr(1, Arr("a", "b", "c"), 3)
        )
    );
    
    Console.WriteLine(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.Println(err)
    } else {
    	fmt.Println(result)
    }
    true
    System.out.println(
        client.query(
            ContainsPath(
                Arr(Value("b"), Value("two")),
                Obj(
                    "a", Value(1),
                    "b", Obj("two", Value(2)),
                    "c", Value(3)
                )
            )
        )
        .get());
    true
    client.query(
      q.ContainsPath(
        [1, 2],
        [1, ["a", "b", "c"], 3]
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.log('Error:', err))
    true
    print(client.query(
      q.contains_path(
        [1, 2],
        [1, ["a", "b", "c"], 3]
      )
    ))
    True
    println(Await.result(
        client.query(
            ContainsPath(
                Arr(1, 2),
                Arr(1, Arr("a", "b", "c"), 3)
            )
        ),
        5.seconds
    ))
    true
  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:

    Value result = await client.Query(
        ContainsPath(
            Path("b", "two"),
            Obj(
                "a", 1,
                "b", Obj("two", 2),
                "c", 3
            )
        )
    );
    
    Console.WriteLine(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.Println(err)
    } else {
    	fmt.Println(result)
    }
    true
    System.out.println(
        client.query(
            ContainsPath(
                Arr(Value("b"), Value("two")),
                Obj(
                    "a", Value(1),
                    "b", Obj("two", Value(2)),
                    "c", Value(3)
                )
            )
        )
        .get());
    true
    client.query(
      q.ContainsPath(
        ['b', 'two'],
        {
          a: 1,
          b: { two: 2, },
          c: 3
        },
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.log('Error:', err))
    true
    print(client.query(
      q.contains_path(
        ["b", "two"],
        {
          "a": 1,
          "b": {"two": 2},
          "c": 3
        }
      )
    ))
    True
    println(Await.result(
        client.query(
            ContainsPath(
                Arr("b", "two"),
                Obj(
                    "a" -> 1,
                    "b" -> Obj("two" -> 2),
                    "c" -> 3
                )
            )
        ),
        5.seconds
    ))
    true
  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:

    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);
    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.Println(err)
    } else {
    	fmt.Println(result)
    }
    true
    System.out.println(
        client.query(
            ContainsPath(
                Arr(Value(2), Value("first")),
                Arr(
                    Obj("first", Value("Alan"), "last", Value("Perlis")),
                    Obj("first", Value("Alan"), "last", Value("Turing")),
                    Obj("first", Value("Grace"), "last", Value("Hopper"))
                )
            )
        )
        .get());
    true
    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.log('Error:', err))
    true
    print(client.query(
      q.contains_path(
        [2, "first"],
        [
          { "first": "Alan", "last": "Perlis" },
          { "first": "Alan", "last": "Turing" },
          { "first": "Grace", "last": "Hopper" }
        ]
      )
    ))
    True
    println(Await.result(
        client.query(
            ContainsPath(
                Arr(2, "first"),
                Arr(
                    Obj("first" -> "Alan", "last" -> "Perlis"),
                    Obj("first" -> "Alan", "last" -> "Turing"),
                    Obj("first" -> "Grace", "last" -> "Hopper")
                )
            )
        ),
        5.seconds
    ))
    true

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!