Product SiteDocumentation Site

5.6.9. Ordered and Unordered expressions

OrderedExpr
Figure 5.45. OrderedExpr

UnorderedExpr
Figure 5.46. UnorderedExpr

By default, the order in which a for clause binds its items is important.
This behaviour can be relaxed in order give the optimizer more leeway. An unordered expression relaxes ordering by for clauses within its operand scope:
Example 5.125. An unordered expression.
unordered {
  for $captain in collection("captains")
  where $captain.century eq 24
  return $captain
}
Result (run with Zorba):
{ "name" : "Jean-Luc Picard", "series" : [ "The next generation" ], "century" : 24 } { "name" : "Benjamin Sisko", "series" : [ "The next generation", "Deep Space 9" ], "century" : 24 } { "name" : "Kathryn Janeway", "series" : [ "The next generation", "Voyager" ], "century" : 24 } { "codename" : "Emergency Command Hologram", "surname" : "The Doctor", "series" : [ "Voyager" ], "century" : 24 }

An ordered expression can be used to reactivate ordering behaviour in a subscope.
Example 5.126. An ordered expression.
unordered {
  for $captain in collection("captains")
  where ordered { exists(for $movie at $i in collection("movies")
                         where $i eq 5
                         where $movie.captain eq $captain.name
                         return $movie) }
  return $captain
}
Result (run with Zorba):
{ "name" : "James T. Kirk", "series" : [ "The original series" ], "century" : 23 }