Options
Origin provides a DSL on the queryable for building options to go along
with the standard queries. The options hash can always be accessed from
a queryable by calling Queryable#options
.
Standard
The following is a table of all options operations and their corresponding MongoDB options hash.
Operation | Origin | Options |
---|---|---|
Queryable#batch_size
Changes the number of documents that are returned by the cursor at a time. |
queryable.batch_size(1000) |
{ batch_size: 1000 } |
Queryable#hint
Adds a hint for which index (or not) to use in the query. |
queryable.hint("$natural" => 1) |
{ hint: { "$natural" => 1 }} |
Queryable#limit
Limit the number of returned documents. |
queryable.limit(100) |
{ limit: 100 } |
Queryable#max_scan
Limit the number of scanned documents in the query. |
queryable.max_scan(100) |
{ max_scan: 100 } |
Queryable#no_timeout
Tell the query not to timeout. |
queryable.no_timeout |
{ timeout: false } |
Queryable#only
Limit the fields that are returned to the provided fields.
Note that you can only include fields (with |
queryable.only(:name, :age) |
{ fields: { "name" => 1, "age" => 1 }} |
Queryable#skip
Skip the provided number of documents. |
queryable.skip(100) |
{ skip: 100 } |
Queryable#slice
Limit the number of elements in embedded arrays that are returned. |
queryable.slice(aliases: [ 0, 5 ]) |
{ aliases: { "$slice" => [ 0, 5 ] }} |
Queryable#snapshot
Set the option to execute the query in snapshot mode. |
queryable.snapshot |
{ snapshot: true } |
Queryable#without
Limit the fields that are returned everything except the
provided fields. Note that you can only include fields
(with |
queryable.without(:name, :age) |
{ fields: { "name" => -1, "age" => -1 }} |
Sorting
Origin's sorting syntax is special in that there are many different ways to add sorting criteria, and the underlying options can be configured to change depending on which driver you are using. You can mix and match ascending and descending in different styles if your heart desires.
Sort Ascending
There are a few different ways to sort fields in ascending order. Choose the syntax you like the best.
queryable.asc(:first_name, :last_name) queryable.order_by(:first_name.asc, :last_name.asc) queryable.order_by("first_name ASC, last_name ASC") queryable.order_by(first_name: 1, last_name: 1) queryable.order_by(first_name: "asc", last_name: "asc") queryable.order_by(first_name: :asc, last_name: :asc) queryable.order_by([[ :first_name, 1 ], [ :last_name, 1 ]]) queryable.order_by([[ :first_name, :asc ], [ :last_name, :asc ]])
All of the previous calls translate to the following options:
{ sort: { "first_name" => 1, "last_name" => 1 }}
Sort Descending
There are a few different ways to sort fields in descending order. Choose the syntax you like the best.
queryable.desc(:first_name, :last_name) queryable.order_by(:first_name.desc, :last_name.desc) queryable.order_by("first_name DESC, last_name DESC") queryable.order_by(first_name: -1, last_name: -1) queryable.order_by(first_name: "desc", last_name: "desc") queryable.order_by(first_name: :desc, last_name: :desc) queryable.order_by([[ :first_name, -1 ], [ :last_name, -1 ]]) queryable.order_by([[ :first_name, :desc ], [ :last_name, :desc ]])
All of the previous calls translate to the following options:
{ sort: { "first_name" => -1, "last_name" => -1 }}
Changing Driver Syntax
By default the sorting syntax is for Moped. If you are using the
10gen driver by itself, then when creating the queryable tell it
to use 10gen's syntax by passing :mongo
as the third
argument to the queryable constructor.
queryable = Query.new({}, {}, :mongo) queryable = queryable.order_by([[ :first_name, :desc ], [ :last_name, :desc ]]) queryable.options #=> { sort: [[ "first_name", -1 ], [ "last_name", -1 ]]}