JSONiq follows the W3C standard for ordered and unordered expressions. The following explanations, provided as an informal summary for convenience, are non-normative.
Figure 62. OrderedExpr
Figure 63. 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 138. 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 139. 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 }