Module Mongoid::Criterion::Optional
In: lib/mongoid/criterion/optional.rb

Methods

asc   ascending   cache   cached?   desc   descending   extras   for_ids   limit   offset   order   order_by   skip   type  

Public Instance methods

asc(*fields)

Alias for ascending

Adds fields to be sorted in ascending order. Will add them in the order they were passed into the method.

@example Sort in ascending order.

  criteria.ascending(:title, :dob)
  criteria.asc(:title, :dob)

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

@return [ Criteria ] The cloned criteria.

Tells the criteria that the cursor that gets returned needs to be cached. This is so multiple iterations don‘t hit the database multiple times, however this is not advisable when working with large data sets as the entire results will get stored in memory.

@example Flag the criteria as cached.

  criteria.cache

@return [ Criteria ] The cloned criteria.

Will return true if the cache option has been set.

@example Is the criteria cached?

  criteria.cached?

@return [ true, false ] If the criteria is flagged as cached.

desc(*fields)

Alias for descending

Adds fields to be sorted in descending order. Will add them in the order they were passed into the method.

@example Sort the criteria in descending order.

  criteria.descending(:title, :dob)
  criteria.desc(:title, :dob)

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

@return [ Criteria ] The cloned criteria.

Adds a criterion to the Criteria that specifies additional options to be passed to the Ruby driver, in the exact format for the driver.

@example Add extra params to the criteria.

  criteria.extras(:limit => 20, :skip => 40)

@param [ Hash ] extras The extra driver options.

@return [ Criteria ] The cloned criteria.

Adds a criterion to the Criteria that specifies an id that must be matched.

@example Add a single id criteria.

  criteria.for_ids("4ab2bc4b8ad548971900005c")

@example Add multiple id criteria.

  criteria.for_ids(["4ab2bc4b8ad548971900005c", "4c454e7ebf4b98032d000001"])

@param [ Array ] ids: A single id or an array of ids.

@return [ Criteria ] The cloned criteria.

Adds a criterion to the Criteria that specifies the maximum number of results to return. This is mostly used in conjunction with skip() to handle paginated results.

@example Limit the result set size.

  criteria.limit(100)

@param [ Integer ] value The max number of results.

@return [ Criteria ] The cloned criteria.

Returns the offset option. If a per_page option is in the list then it will replace it with a skip parameter and return the same value. Defaults to 20 if nothing was provided.

@example Get the offset.

  criteria.offset(10)

@return [ Integer ] The number of documents to skip.

order(*args)

Alias for order_by

Adds a criterion to the Criteria that specifies the sort order of the returned documents in the database. Similar to a SQL "ORDER BY".

@example Order by specific fields.

  criteria.order_by([[:field1, :asc], [:field2, :desc]])

@param [ Array ] params: An Array of [field, direction] sorting pairs.

@return [ Criteria ] The cloned criteria.

Adds a criterion to the Criteria that specifies how many results to skip when returning Documents. This is mostly used in conjunction with limit() to handle paginated results, and is similar to the traditional "offset" parameter.

@example Skip a specified number of documents.

  criteria.skip(20)

@param [ Integer ] value The number of results to skip.

@return [ Criteria ] The cloned criteria.

Adds a criterion to the Criteria that specifies a type or an Array of types that must be matched.

@example Match only specific models.

  criteria.type('Browser')
  criteria.type(['Firefox', 'Browser'])

@param [ Array<String> ] types The types to match against.

@return [ Criteria ] The cloned criteria.

[Validate]