Module: Mongoid::Criteria::Queryable::Optional
- Extended by:
- Macroable
- Included in:
- Mongoid::Criteria::Queryable
- 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.
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
- #options The query options.(Thequeryoptions.) ⇒ Object
Class Method Summary collapse
-
.forwardables ⇒ Array<Symbol>
Get the methods on the optional that can be forwarded to from a model.
Instance Method Summary collapse
-
#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.
-
#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
deprecated
Deprecated.
The max_scan option is deprecated in MongoDB 4.0 and later. Use max_time_ms instead.
-
#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.
-
#only(*args) ⇒ Optional
Limits the results to only contain the fields provided.
-
#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.
Methods included from Macroable
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 12 def @options end |
#options The query options.(Thequeryoptions.) ⇒ Object
12 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 12 attr_accessor :options |
Class Method Details
.forwardables ⇒ Array<Symbol>
Get the methods on the optional that can be forwarded to from a model.
373 374 375 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 373 def forwardables public_instance_methods(false) - %i[options options=] end |
Instance Method Details
#ascending(*fields) ⇒ Optional Also known as: asc
Add ascending sorting options for all the provided fields.
22 23 24 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 22 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.
38 39 40 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 38 def batch_size(value = nil) option(value) { || .store(:batch_size, value) } end |
#collation(collation_doc) ⇒ Optional
Set the collation.
305 306 307 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 305 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.
276 277 278 279 280 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 276 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.
293 294 295 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 293 def cursor_type(type) clone.tap { |query| query..store(:cursor_type, type) } end |
#descending(*fields) ⇒ Optional Also known as: desc
Add descending sorting options for all the provided fields.
50 51 52 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 50 def descending(*fields) sort_with_list(*fields, -1) end |
#hint(value = nil) ⇒ Optional
Add an index hint to the query options.
65 66 67 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 65 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.
77 78 79 80 81 82 83 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 77 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
The max_scan option is deprecated in MongoDB 4.0 and later. Use max_time_ms instead.
Adds the option to limit the number of documents scanned in the collection.
97 98 99 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 97 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.
110 111 112 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 110 def max_time_ms(value = nil) option(value) { || .store(:max_time_ms, value) } end |
#no_timeout ⇒ Optional
Tell the query not to timeout.
120 121 122 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 120 def no_timeout clone.tap { |query| query..store(:timeout, false) } end |
#only(*args) ⇒ Optional
Limits the results to only contain the fields provided.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 132 def only(*args) args = args.flatten option(*args) do || .store( :fields, args.inject([:fields] || {}) { |sub, field| sub.tap { sub[field] = 1 } }, false ) end end |
#order_by(*spec) ⇒ Optional Also known as: order
Adds sorting criterion to the options.
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 172 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.
193 194 195 196 197 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 193 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.
207 208 209 210 211 212 213 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 207 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.
224 225 226 227 228 229 230 231 232 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 224 def slice(criterion = nil) option(criterion) do || .__union__( fields: criterion.inject({}) do |option, (field, val)| option.tap { |opt| opt.store(field, { '$slice' => val }) } end ) end end |
#snapshot ⇒ Optional
Tell the query to operate in snapshot mode.
240 241 242 243 244 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 240 def snapshot clone.tap do |query| query..store(:snapshot, true) end end |
#without(*args) ⇒ Optional
Limits the results to only contain the fields not provided.
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/mongoid/criteria/queryable/optional.rb', line 254 def without(*args) args = args.flatten option(*args) do || .store( :fields, args.inject([:fields] || {}) { |sub, field| sub.tap { sub[field] = 0 } }, false ) end end |