Module Origin::Optional
In: lib/origin/optional.rb

The optional module includes all behaviour that has to do with extra options surrounding queries, like skip, limit, sorting, etc.

Methods

asc   ascending   batch_size   collation   comment   cursor_type   desc   descending   hint   limit   max_scan   no_timeout   offset   only   order   order_by   reorder   skip   slice   snapshot   without  

Attributes

options  [RW]  @attribute [rw] options The query options.

Public Instance methods

asc(*fields)

Alias for ascending

Add ascending sorting options for all the provided fields.

@example Add ascending sorting.

  optional.ascending(:first_name, :last_name)

@param [ Array<Symbol> ] fields The fields to sort.

@return [ Optional ] The cloned optional.

@since 1.0.0

Adds the option for telling MongoDB how many documents to retrieve in it‘s batching.

@example Apply the batch size options.

  optional.batch_size(500)

@param [ Integer ] value The batch size.

@return [ Optional ] The cloned optional.

@since 1.0.0

Set the collation. Can only be used with server versions >= 3.4.

@example Set the collation.

  optional.collation(locale: 'fr', strength: 2)

@param [ Hash ] collation_doc The document describing the collation to use.

@return [ Optional ] The cloned optional.

@since 2.3.0

Associate a comment with the query.

@example Add a comment.

  optional.comment('slow query')

@note Set profilingLevel to 2 and the comment will be logged in the profile

  collection along with the query.

@param [ String ] comment The comment to be associated with the query.

@return [ Optional ] The cloned optional.

@since 2.2.0

Set the cursor type.

@example Set the cursor type.

  optional.cursor_type(:tailable)
  optional.cursor_type(:tailable_await)

@note The cursor can be type :tailable or :tailable_await.

@param [ Symbol ] type The type of cursor to create.

@return [ Optional ] The cloned optional.

@since 2.2.0

desc(*fields)

Alias for descending

Add descending sorting options for all the provided fields.

@example Add descending sorting.

  optional.descending(:first_name, :last_name)

@param [ Array<Symbol> ] fields The fields to sort.

@return [ Optional ] The cloned optional.

@since 1.0.0

Add an index hint to the query options.

@example Add an index hint.

  optional.hint("$natural" => 1)

@param [ Hash ] value The index hint.

@return [ Optional ] The cloned optional.

@since 1.0.0

Add the number of documents to limit in the returned results.

@example Limit the number of returned documents.

  optional.limit(20)

@param [ Integer ] value The number of documents to return.

@return [ Optional ] The cloned optional.

@since 1.0.0

Adds the option to limit the number of documents scanned in the collection.

@example Add the max scan limit.

  optional.max_scan(1000)

@param [ Integer ] value The max number of documents to scan.

@return [ Optional ] The cloned optional.

@since 1.0.0

Tell the query not to timeout.

@example Tell the query not to timeout.

  optional.no_timeout

@return [ Optional ] The cloned optional.

@since 1.0.0

offset(value = nil)

Alias for skip

Limits the results to only contain the fields provided.

@example Limit the results to the provided fields.

  optional.only(:name, :dob)

@param [ Array<Symbol> ] args The fields to return.

@return [ Optional ] The cloned optional.

@since 1.0.0

order(*spec)

Alias for order_by

Adds sorting criterion to the options.

@example Add sorting options via a hash with integer directions.

  optional.order_by(name: 1, dob: -1)

@example Add sorting options via a hash with symbol directions.

  optional.order_by(name: :asc, dob: :desc)

@example Add sorting options via a hash with string directions.

  optional.order_by(name: "asc", dob: "desc")

@example Add sorting options via an array with integer directions.

  optional.order_by([[ name, 1 ], [ dob, -1 ]])

@example Add sorting options via an array with symbol directions.

  optional.order_by([[ name, :asc ], [ dob, :desc ]])

@example Add sorting options via an array with string directions.

  optional.order_by([[ name, "asc" ], [ dob, "desc" ]])

@example Add sorting options with keys.

  optional.order_by(:name.asc, :dob.desc)

@example Add sorting options via a string.

  optional.order_by("name ASC, dob DESC")

@param [ Array, Hash, String ] spec The sorting specification.

@return [ Optional ] The cloned optional.

@since 1.0.0

Instead of merging the order criteria, use this method to completely replace the existing ordering with the provided.

@example Replace the ordering.

  optional.reorder(name: :asc)

@param [ Array, Hash, String ] spec The sorting specification.

@return [ Optional ] The cloned optional.

@since 2.1.0

Add the number of documents to skip.

@example Add the number to skip.

  optional.skip(100)

@param [ Integer ] value The number to skip.

@return [ Optional ] The cloned optional.

@since 1.0.0

Limit the returned results via slicing embedded arrays.

@example Slice the returned results.

  optional.slice(aliases: [ 0, 5 ])

@param [ Hash ] criterion The slice options.

@return [ Optional ] The cloned optional.

@since 1.0.0

Tell the query to operate in snapshot mode.

@example Add the snapshot option.

  optional.snapshot

@return [ Optional ] The cloned optional.

@since 1.0.0

Limits the results to only contain the fields not provided.

@example Limit the results to the fields not provided.

  optional.without(:name, :dob)

@param [ Array<Symbol> ] args The fields to ignore.

@return [ Optional ] The cloned optional.

@since 1.0.0

[Validate]