Module: Mongo::Collection::View::Readable
- Included in:
- Mongo::Collection::View
- Defined in:
- lib/mongo/collection/view/readable.rb
Overview
Defines read related behaviour for collection view.
Constant Summary collapse
- QUERY =
The query modifier constant.
'$query'.freeze
- MODIFIERS =
The modifiers option constant.
'modifiers'.freeze
Instance Method Summary collapse
-
#aggregate(pipeline, options = {}) ⇒ Aggregation
Execute an aggregation on the collection view.
-
#allow_partial_results ⇒ View
Allows the query to get partial results if some shards are down.
-
#await_data ⇒ View
Tell the query’s cursor to stay open and wait for data.
-
#batch_size(batch_size = nil) ⇒ Integer, View
The number of documents returned in each batch of results from MongoDB.
-
#comment(comment = nil) ⇒ String, View
Associate a comment with the query.
-
#count(options = {}) ⇒ Integer
Get a count of matching documents in the collection.
-
#distinct(field_name, options = {}) ⇒ Array<Object>
Get a list of distinct values for a specific field.
-
#hint(hint = nil) ⇒ Hash, View
The index that MongoDB will be forced to use for the query.
-
#limit(limit = nil) ⇒ Integer, View
The max number of docs to return from the query.
-
#map_reduce(map, reduce, options = {}) ⇒ MapReduce
Execute a map/reduce operation on the collection view.
-
#max_await_time_ms(max = nil) ⇒ Integer, View
A cumulative time limit in milliseconds for processing get more operations on a cursor.
-
#max_scan(value = nil) ⇒ Integer, View
Set the max number of documents to scan.
-
#max_time_ms(max = nil) ⇒ Integer, View
A cumulative time limit in milliseconds for processing operations on a cursor.
-
#max_value(value = nil) ⇒ Hash, View
Set the maximum value to search.
-
#min_value(value = nil) ⇒ Hash, View
Set the minimum value to search.
-
#modifiers(doc = nil) ⇒ Hash, View
“meta” operators that let you modify the output or behavior of a query.
-
#no_cursor_timeout ⇒ View
The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use.
-
#projection(document = nil) ⇒ Hash, View
The fields to include or exclude from each doc in the result set.
-
#read(value = nil) ⇒ Symbol, View
The read preference to use for the query.
-
#return_key(value = nil) ⇒ true, ...
Set whether to return only the indexed field or fields.
-
#show_disk_loc(value = nil) ⇒ true, ...
(also: #show_record_id)
Set whether the disk location should be shown for each document.
-
#skip(number = nil) ⇒ Integer, View
The number of docs to skip before returning results.
-
#snapshot(value = nil) ⇒ Object
Set the snapshot value for the view.
-
#sort(spec = nil) ⇒ Hash, View
The key and direction pairs by which the result set will be sorted.
Instance Method Details
#aggregate(pipeline, options = {}) ⇒ Aggregation
Execute an aggregation on the collection view.
47 48 49 |
# File 'lib/mongo/collection/view/readable.rb', line 47 def aggregate(pipeline, = {}) Aggregation.new(self, pipeline, ) end |
#allow_partial_results ⇒ View
Allows the query to get partial results if some shards are down.
59 60 61 |
# File 'lib/mongo/collection/view/readable.rb', line 59 def allow_partial_results configure(:allow_partial_results, true) end |
#await_data ⇒ View
Tell the query’s cursor to stay open and wait for data.
71 72 73 |
# File 'lib/mongo/collection/view/readable.rb', line 71 def await_data configure(:await_data, true) end |
#batch_size(batch_size = nil) ⇒ Integer, View
Specifying 1 or a negative number is analogous to setting a limit.
The number of documents returned in each batch of results from MongoDB.
new View
.
88 89 90 |
# File 'lib/mongo/collection/view/readable.rb', line 88 def batch_size(batch_size = nil) configure(:batch_size, batch_size) end |
#comment(comment = nil) ⇒ String, View
Set profilingLevel to 2 and the comment will be logged in the profile collection along with the query.
Associate a comment with the query.
106 107 108 |
# File 'lib/mongo/collection/view/readable.rb', line 106 def comment(comment = nil) configure(:comment, comment) end |
#count(options = {}) ⇒ Integer
Get a count of matching documents in the collection.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mongo/collection/view/readable.rb', line 128 def count( = {}) cmd = { :count => collection.name, :query => filter } cmd[:skip] = [:skip] if [:skip] cmd[:hint] = [:hint] if [:hint] cmd[:limit] = [:limit] if [:limit] cmd[:maxTimeMS] = [:max_time_ms] if [:max_time_ms] cmd[:readConcern] = collection.read_concern if collection.read_concern read_with_retry do database.command(cmd, ).n.to_i end end |
#distinct(field_name, options = {}) ⇒ Array<Object>
Get a list of distinct values for a specific field.
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mongo/collection/view/readable.rb', line 155 def distinct(field_name, ={}) cmd = { :distinct => collection.name, :key => field_name.to_s, :query => filter } cmd[:maxTimeMS] = [:max_time_ms] if [:max_time_ms] cmd[:readConcern] = collection.read_concern if collection.read_concern read_with_retry do database.command(cmd, ).first['values'] end end |
#hint(hint = nil) ⇒ Hash, View
The index that MongoDB will be forced to use for the query.
176 177 178 |
# File 'lib/mongo/collection/view/readable.rb', line 176 def hint(hint = nil) configure(:hint, hint) end |
#limit(limit = nil) ⇒ Integer, View
The max number of docs to return from the query.
190 191 192 |
# File 'lib/mongo/collection/view/readable.rb', line 190 def limit(limit = nil) configure(:limit, limit) end |
#map_reduce(map, reduce, options = {}) ⇒ MapReduce
Execute a map/reduce operation on the collection view.
206 207 208 |
# File 'lib/mongo/collection/view/readable.rb', line 206 def map_reduce(map, reduce, = {}) MapReduce.new(self, map, reduce, ) end |
#max_await_time_ms(max = nil) ⇒ Integer, View
A cumulative time limit in milliseconds for processing get more operations on a cursor.
402 403 404 |
# File 'lib/mongo/collection/view/readable.rb', line 402 def max_await_time_ms(max = nil) configure(:max_await_time_ms, max) end |
#max_scan(value = nil) ⇒ Integer, View
Set the max number of documents to scan.
220 221 222 |
# File 'lib/mongo/collection/view/readable.rb', line 220 def max_scan(value = nil) configure(:max_scan, value) end |
#max_time_ms(max = nil) ⇒ Integer, View
A cumulative time limit in milliseconds for processing operations on a cursor.
416 417 418 |
# File 'lib/mongo/collection/view/readable.rb', line 416 def max_time_ms(max = nil) configure(:max_time_ms, max) end |
#max_value(value = nil) ⇒ Hash, View
Set the maximum value to search.
234 235 236 |
# File 'lib/mongo/collection/view/readable.rb', line 234 def max_value(value = nil) configure(:max_value, value) end |
#min_value(value = nil) ⇒ Hash, View
Set the minimum value to search.
248 249 250 |
# File 'lib/mongo/collection/view/readable.rb', line 248 def min_value(value = nil) configure(:min_value, value) end |
#modifiers(doc = nil) ⇒ Hash, View
“meta” operators that let you modify the output or behavior of a query.
386 387 388 389 |
# File 'lib/mongo/collection/view/readable.rb', line 386 def modifiers(doc = nil) return Builder::Modifiers.map_server_modifiers() if doc.nil? new(.merge(Builder::Modifiers.(doc))) end |
#no_cursor_timeout ⇒ View
The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that.
261 262 263 |
# File 'lib/mongo/collection/view/readable.rb', line 261 def no_cursor_timeout configure(:no_cursor_timeout, true) end |
#projection(document = nil) ⇒ Hash, View
A value of 0 excludes a field from the doc. A value of 1 includes it. Values must all be 0 or all be 1, with the exception of the _id value. The _id field is included by default. It must be excluded explicitly.
The fields to include or exclude from each doc in the result set.
279 280 281 282 |
# File 'lib/mongo/collection/view/readable.rb', line 279 def projection(document = nil) validate_doc!(document) if document configure(:projection, document) end |
#read(value = nil) ⇒ Symbol, View
If none is specified for the query, the read preference of the collection will be used.
The read preference to use for the query.
295 296 297 298 299 |
# File 'lib/mongo/collection/view/readable.rb', line 295 def read(value = nil) return default_read if value.nil? selector = value.is_a?(Hash) ? ServerSelector.get(client..merge(value)) : value configure(:read, selector) end |
#return_key(value = nil) ⇒ true, ...
Set whether to return only the indexed field or fields.
311 312 313 |
# File 'lib/mongo/collection/view/readable.rb', line 311 def return_key(value = nil) configure(:return_key, value) end |
#show_disk_loc(value = nil) ⇒ true, ... Also known as: show_record_id
Set whether the disk location should be shown for each document.
326 327 328 |
# File 'lib/mongo/collection/view/readable.rb', line 326 def show_disk_loc(value = nil) configure(:show_disk_loc, value) end |
#skip(number = nil) ⇒ Integer, View
The number of docs to skip before returning results.
342 343 344 |
# File 'lib/mongo/collection/view/readable.rb', line 342 def skip(number = nil) configure(:skip, number) end |
#snapshot(value = nil) ⇒ Object
When set to true, prevents documents from returning more than once.
Set the snapshot value for the view.
357 358 359 |
# File 'lib/mongo/collection/view/readable.rb', line 357 def snapshot(value = nil) configure(:snapshot, value) end |
#sort(spec = nil) ⇒ Hash, View
The key and direction pairs by which the result set will be sorted.
372 373 374 |
# File 'lib/mongo/collection/view/readable.rb', line 372 def sort(spec = nil) configure(:sort, spec) end |