Product SiteDocumentation Site

10.9. Composing FLWOR Expressions

Like all other expressions, FLWOR expressions can be composed. In the following example, a predicate expression is nested in an existential quantifier, nested in the where clause of a FLWOR, nested in a function call, nested in a FLWOR, nested in a function call, nested in an array constructor. The examples looks for users who got an answer not accepted, but for whom there were at least two questions for which they gave an answer with a better score.
Example 10.23. Nested FLWORs.
[
  distinct-values(
    for $answer in collection("answers")
    let $oid := $answer.owner.user_id
    where count(
      for $question in collection("faqs")
      where
        some $other-answer
        in collection("answers")
           [$$.question_id eq
                $question.question_id
            and
            $$.owner.user_id eq $oid]
        satisfies
          $other-answer.score gt $answer.score
       return $question
    ) ge 2
    where not $answer.is_accepted
    return $answer.owner.display_name
  )
]
Results:
[ "JasonSmith" ]