OrderBy specifies that the returned documents appear sorted by the given field in the given direction. A query can have at most one OrderBy clause. If it has none, the order of returned documents is unspecified. If a query has a Where clause and an OrderBy clause, the OrderBy clause's field must app
(field, direction string)
| 171 | // must appear in a Where clause. |
| 172 | // It is an error to specify OrderBy in a Delete or Update query. |
| 173 | func (q *Query) OrderBy(field, direction string) *Query { |
| 174 | if q.err != nil { |
| 175 | return q |
| 176 | } |
| 177 | if field == "" { |
| 178 | return q.invalidf("OrderBy: empty field") |
| 179 | } |
| 180 | if direction != Ascending && direction != Descending { |
| 181 | return q.invalidf("OrderBy: direction must be one of %q or %q", Ascending, Descending) |
| 182 | } |
| 183 | if q.dq.OrderByField != "" { |
| 184 | return q.invalidf("a query can have at most one OrderBy") |
| 185 | } |
| 186 | q.dq.OrderByField = field |
| 187 | q.dq.OrderAscending = (direction == Ascending) |
| 188 | return q |
| 189 | } |
| 190 | |
| 191 | // BeforeQuery takes a callback function that will be called before the Query is |
| 192 | // executed to the underlying service's query functionality. The callback takes |