Module: Mongoid::Criteria::Queryable::Optional
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Macroable
|
|
Defined in: | lib/mongoid/criteria/queryable/optional.rb |
Overview
The optional module includes all behavior that has to do with extra options surrounding queries, like skip, limit, sorting, etc.
Class Method Summary
-
.forwardables ⇒ Array<Symbol>
Get the methods on the optional that can be forwarded to from a model.
Macroable
- Extended
Instance Attribute Summary
Instance Method Summary
-
#asc(*fields)
Alias for #ascending.
-
#ascending(*fields) ⇒ Optional
(also: #asc)
Add ascending sorting options for all the provided fields.
-
#batch_size(value = nil) ⇒ Optional
Adds the option for telling MongoDB how many documents to retrieve in it’s batching.
-
#collation(collation_doc) ⇒ Optional
::Set
the collation. -
#comment(comment = nil) ⇒ Optional
Associate a comment with the query.
-
#cursor_type(type) ⇒ Optional
::Set
the cursor type. -
#desc(*fields)
Alias for #descending.
-
#descending(*fields) ⇒ Optional
(also: #desc)
Add descending sorting options for all the provided fields.
-
#hint(value = nil) ⇒ Optional
Add an index hint to the query options.
-
#limit(value = nil) ⇒ Optional
Add the number of documents to limit in the returned results.
-
#max_scan(value = nil) ⇒ Optional
Adds the option to limit the number of documents scanned in the collection.
-
#max_time_ms(value = nil) ⇒ Optional
Adds a cumulative time limit in milliseconds for processing operations on a cursor.
-
#no_timeout ⇒ Optional
Tell the query not to timeout.
-
#offset(value = nil)
Alias for #skip.
-
#only(*args) ⇒ Optional
Limits the results to only contain the fields provided.
-
#order(*spec)
Alias for #order_by.
-
#order_by(*spec) ⇒ Optional
(also: #order)
Adds sorting criterion to the options.
-
#reorder(*spec) ⇒ Optional
Instead of merging the order criteria, use this method to completely replace the existing ordering with the provided.
-
#skip(value = nil) ⇒ Optional
(also: #offset)
Add the number of documents to skip.
-
#slice(criterion = nil) ⇒ Optional
Limit the returned results via slicing embedded arrays.
-
#snapshot ⇒ Optional
Tell the query to operate in snapshot mode.
-
#without(*args) ⇒ Optional
Limits the results to only contain the fields not provided.
-
#add_sort_option(options, field, direction) ⇒ Optional
private
Internal use only
Internal use only
Add a single sort option.
-
#option(*args) ⇒ Queryable
private
Internal use only
Internal use only
Take the provided criterion and store it as an option in the query options.
-
#sort_with_list(*fields, direction) ⇒ Optional
private
Internal use only
Internal use only
Add multiple sort options at once.
Class Method Details
.forwardables ⇒ Array<Symbol>
Get the methods on the optional that can be forwarded to from a model.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 374
def forwardables public_instance_methods(false) - [ :, : ] end
Instance Attribute Details
#options (rw)
[ GitHub ]# File 'lib/mongoid/criteria/queryable/optional.rb', line 14
attr_accessor :
#options The query options.(The query options.) (rw)
[ GitHub ]# File 'lib/mongoid/criteria/queryable/optional.rb', line 14
attr_accessor :
Instance Method Details
#add_sort_option(options, field, direction) ⇒ Optional
(private)
Add a single sort option.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 321
def add_sort_option(, field, direction) sorting = ( [:sort] || {}).dup sorting[field] = direction .store(:sort, sorting) end
#asc(*fields)
Alias for #ascending.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 27
alias :asc :ascending
#ascending(*fields) ⇒ Optional
Also known as: #asc
Add ascending sorting options for all the provided fields.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 24
def ascending(*fields) sort_with_list(*fields, 1) end
#batch_size(value = nil) ⇒ Optional
Adds the option for telling MongoDB how many documents to retrieve in it’s batching.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 40
def batch_size(value = nil) option(value) { || .store(:batch_size, value) } end
#collation(collation_doc) ⇒ Optional
::Set
the collation.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 303
def collation(collation_doc) clone.tap { |query| query. .store(:collation, collation_doc) } end
#comment(comment = nil) ⇒ Optional
::Set
profilingLevel to 2 and the comment will be logged in the profile collection along with the query.
Associate a comment with the query.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 274
def comment(comment = nil) clone.tap do |query| query. .store(:comment, comment) end end
#cursor_type(type) ⇒ Optional
The cursor can be type :tailable
or :tailable_await
.
::Set
the cursor type.
#desc(*fields)
Alias for #descending.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 55
alias :desc :descending
#descending(*fields) ⇒ Optional
Also known as: #desc
Add descending sorting options for all the provided fields.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 52
def descending(*fields) sort_with_list(*fields, -1) end
#hint(value = nil) ⇒ Optional
Add an index hint to the query options.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 67
def hint(value = nil) option(value) { || .store(:hint, value) } end
#limit(value = nil) ⇒ Optional
Add the number of documents to limit in the returned results.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 79
def limit(value = nil) option(value) do |, query| val = value.to_i .store(:limit, val) query.pipeline.push("$limit" => val) if aggregating? end end
#max_scan(value = nil) ⇒ Optional
Adds the option to limit the number of documents scanned in the collection.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 96
def max_scan(value = nil) option(value) { || .store(:max_scan, value) } end
#max_time_ms(value = nil) ⇒ Optional
Adds a cumulative time limit in milliseconds for processing operations on a cursor.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 108
def max_time_ms(value = nil) option(value) { || .store(:max_time_ms, value) } end
#no_timeout ⇒ Optional
Tell the query not to timeout.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 118
def no_timeout clone.tap { |query| query. .store(:timeout, false) } end
#offset(value = nil)
Alias for #skip.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 212
alias :offset :skip
#only(*args) ⇒ Optional
Limits the results to only contain the fields provided.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 130
def only(*args) args = args.flatten option(*args) do || .store( :fields, args.inject( [:fields] || {}){ |sub, field| sub.tap { sub[field] = 1 }}, false ) end end
#option(*args) ⇒ Queryable (private)
Take the provided criterion and store it as an option in the query options.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 338
def option(*args) clone.tap do |query| unless args.compact.empty? yield(query., query) end end end
#order(*spec)
Alias for #order_by.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 180
alias :order :order_by
#order_by(*spec) ⇒ Optional
Also known as: #order
Adds sorting criterion to the options.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 170
def order_by(*spec) option(spec) do |, query| spec.compact.each do |criterion| criterion.__sort_option__.each_pair do |field, direction| add_sort_option(, field, direction) end query.pipeline.push("$sort" => [:sort]) if aggregating? end end end
#reorder(*spec) ⇒ Optional
Instead of merging the order criteria, use this method to completely replace the existing ordering with the provided.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 191
def reorder(*spec) clone.tap do |query| query. .delete(:sort) end.order_by(*spec) end
#skip(value = nil) ⇒ Optional
Also known as: #offset
Add the number of documents to skip.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 205
def skip(value = nil) option(value) do |, query| val = value.to_i .store(:skip, val) query.pipeline.push("$skip" => val) if aggregating? end end
#slice(criterion = nil) ⇒ Optional
Limit the returned results via slicing embedded arrays.
#snapshot ⇒ Optional
Tell the query to operate in snapshot mode.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 238
def snapshot clone.tap do |query| query. .store(:snapshot, true) end end
#sort_with_list(*fields, direction) ⇒ Optional
(private)
Add multiple sort options at once.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 357
def sort_with_list(*fields, direction) option(fields) do |, query| fields.flatten.compact.each do |field| add_sort_option(, field, direction) end query.pipeline.push("$sort" => [:sort]) if aggregating? end end
#without(*args) ⇒ Optional
Limits the results to only contain the fields not provided.
# File 'lib/mongoid/criteria/queryable/optional.rb', line 252
def without(*args) args = args.flatten option(*args) do || .store( :fields, args.inject( [:fields] || {}){ |sub, field| sub.tap { sub[field] = 0 }}, false ) end end