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.
# File lib/mongoid/criterion/optional.rb, line 15 def ascending(*fields) clone.tap do |crit| crit.options[:sort] = [] unless options[:sort] || fields.first.nil? fields.flatten.each { |field| merge_options(crit.options[:sort], [ field, :asc ]) } end end
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.
# File lib/mongoid/criterion/optional.rb, line 32 def cache clone.tap { |crit| crit.options.merge!(:cache => true) } end
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.
# File lib/mongoid/criterion/optional.rb, line 42 def cached? options[:cache] == true end
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.
# File lib/mongoid/criterion/optional.rb, line 56 def descending(*fields) clone.tap do |crit| crit.options[:sort] = [] unless options[:sort] || fields.first.nil? fields.flatten.each { |field| merge_options(crit.options[:sort], [ field, :desc ]) } end end
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.
# File lib/mongoid/criterion/optional.rb, line 73 def extras(extras) clone.tap do |crit| crit.options.merge!(extras) end end
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.
# File lib/mongoid/criterion/optional.rb, line 90 def for_ids(*ids) ids.flatten! if ids.size > 1 where(:_id.in => ::BSON::ObjectId.convert(klass, ids)) else where(:_id => ids.first) end end
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.
# File lib/mongoid/criterion/optional.rb, line 109 def limit(value = 20) clone.tap { |crit| crit.options[:limit] = value } end
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.
# File lib/mongoid/criterion/optional.rb, line 121 def offset(*args) args.size > 0 ? skip(args.first) : options[:skip] end
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.
# File lib/mongoid/criterion/optional.rb, line 134 def order_by(*args) clone.tap do |crit| arguments = args.size == 1 ? args.first : args crit.options[:sort] = [] unless options[:sort] || args.first.nil? if arguments.is_a?(Array) #[:name, :asc] if arguments.size == 2 && (arguments.first.is_a?(Symbol) || arguments.first.is_a?(String)) build_order_options(arguments, crit) else arguments.each { |argument| build_order_options(argument, crit) } end else build_order_options(arguments, crit) end end end
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.
# File lib/mongoid/criterion/optional.rb, line 163 def skip(value = 0) clone.tap { |crit| crit.options[:skip] = value } end
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.
# File lib/mongoid/criterion/optional.rb, line 177 def type(types) types = [types] unless types.is_a?(Array) any_in(:_type => types) end
Generated with the Darkfish Rdoc Generator 2.