Module: Mongo::Collection::View::Readable
- Included in:
- Mongo::Collection::View
- Defined in:
- lib/mongo/collection/view/readable.rb
Overview
Defines read related behavior 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(opts = {}) ⇒ Integer
deprecated
Deprecated.
Use #count_documents or #estimated_document_count instead. However, note that the following operators will need to be substituted when switching to #count_documents:
* $where should be replaced with $expr (only works on 3.6+) * $near should be replaced with $geoWithin with $center * $nearSphere should be replaced with $geoWithin with $centerSphere
-
#count_documents(opts = {}) ⇒ Integer
Get a count of matching documents in the collection.
-
#cursor_type(type = nil) ⇒ :tailable, ...
The type of cursor to use.
-
#distinct(field_name, opts = {}) ⇒ Array<Object>
Get a list of distinct values for a specific field.
-
#estimated_document_count(opts = {}) ⇒ Integer
Gets an estimate of the count of documents in a collection using collection metadata.
-
#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
deprecated
Deprecated.
This option is deprecated as of MongoDB server version 4.0.
-
#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
deprecated
Deprecated.
This option is deprecated as of MongoDB server version 4.0.
-
#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(opts = {}) ⇒ Integer
Use #count_documents or #estimated_document_count instead. However, note that the following operators will need to be substituted when switching to #count_documents:
* $where should be replaced with $expr (only works on 3.6+)
* $near should be replaced with $geoWithin with $center
* $nearSphere should be replaced with $geoWithin with $centerSphere
Get a count of matching documents in the collection.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mongo/collection/view/readable.rb', line 135 def count(opts = {}) cmd = { :count => collection.name, :query => filter } cmd[:skip] = opts[:skip] if opts[:skip] cmd[:hint] = opts[:hint] if opts[:hint] cmd[:limit] = opts[:limit] if opts[:limit] cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms] cmd[:readConcern] = collection.read_concern if collection.read_concern Mongo::Lint.validate_underscore_read_preference(opts[:read]) read_pref = opts[:read] || read_preference selector = ServerSelector.get(read_pref || server_selector) with_session(opts) do |session| read_with_retry(session) do server = selector.select_server(cluster) apply_collation!(cmd, server, opts) Operation::Count.new({ :selector => cmd, :db_name => database.name, :options => {:limit => -1}, :read => read_pref, :session => session }).execute(server) end.n.to_i end end |
#count_documents(opts = {}) ⇒ Integer
Get a count of matching documents in the collection.
179 180 181 182 183 184 185 186 187 |
# File 'lib/mongo/collection/view/readable.rb', line 179 def count_documents(opts = {}) pipeline = [:'$match' => filter] pipeline << { :'$skip' => opts[:skip] } if opts[:skip] pipeline << { :'$limit' => opts[:limit] } if opts[:limit] pipeline << { :'$group' => { _id: nil, n: { :'$sum' => 1 } } } opts.select! { |k, _| [:hint, :max_time_ms, :read, :collation].include?(k) } aggregate(pipeline, opts).first['n'].to_i end |
#cursor_type(type = nil) ⇒ :tailable, ...
The type of cursor to use. Can be :tailable or :tailable_await.
532 533 534 |
# File 'lib/mongo/collection/view/readable.rb', line 532 def cursor_type(type = nil) configure(:cursor_type, type) end |
#distinct(field_name, opts = {}) ⇒ Array<Object>
Get a list of distinct values for a specific field.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/mongo/collection/view/readable.rb', line 239 def distinct(field_name, opts = {}) cmd = { :distinct => collection.name, :key => field_name.to_s, :query => filter } cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms] cmd[:readConcern] = collection.read_concern if collection.read_concern Mongo::Lint.validate_underscore_read_preference(opts[:read]) read_pref = opts[:read] || read_preference selector = ServerSelector.get(read_pref || server_selector) with_session(opts) do |session| read_with_retry(session) do server = selector.select_server(cluster) apply_collation!(cmd, server, opts) Operation::Distinct.new({ :selector => cmd, :db_name => database.name, :options => {:limit => -1}, :read => read_pref, :session => session }).execute(server) end.first['values'] end end |
#estimated_document_count(opts = {}) ⇒ Integer
Gets an estimate of the count of documents in a collection using collection metadata.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/mongo/collection/view/readable.rb', line 203 def estimated_document_count(opts = {}) cmd = { count: collection.name } cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms] cmd[:readConcern] = collection.read_concern if collection.read_concern Mongo::Lint.validate_underscore_read_preference(opts[:read]) read_pref = opts[:read] || read_preference selector = ServerSelector.get(read_pref || server_selector) with_session(opts) do |session| read_with_retry(session) do server = selector.select_server(cluster) Operation::Count.new( selector: cmd, db_name: database.name, read: read_pref, session: session ).execute(server) end.n.to_i end end |
#hint(hint = nil) ⇒ Hash, View
The index that MongoDB will be forced to use for the query.
273 274 275 |
# File 'lib/mongo/collection/view/readable.rb', line 273 def hint(hint = nil) configure(:hint, hint) end |
#limit(limit = nil) ⇒ Integer, View
The max number of docs to return from the query.
287 288 289 |
# File 'lib/mongo/collection/view/readable.rb', line 287 def limit(limit = nil) configure(:limit, limit) end |
#map_reduce(map, reduce, options = {}) ⇒ MapReduce
Execute a map/reduce operation on the collection view.
303 304 305 |
# File 'lib/mongo/collection/view/readable.rb', line 303 def map_reduce(map, reduce, = {}) MapReduce.new(self, map, reduce, @options.merge()) end |
#max_await_time_ms(max = nil) ⇒ Integer, View
A cumulative time limit in milliseconds for processing get more operations on a cursor.
504 505 506 |
# File 'lib/mongo/collection/view/readable.rb', line 504 def max_await_time_ms(max = nil) configure(:max_await_time_ms, max) end |
#max_scan(value = nil) ⇒ Integer, View
This option is deprecated as of MongoDB server version 4.0.
Set the max number of documents to scan.
320 321 322 |
# File 'lib/mongo/collection/view/readable.rb', line 320 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.
518 519 520 |
# File 'lib/mongo/collection/view/readable.rb', line 518 def max_time_ms(max = nil) configure(:max_time_ms, max) end |
#max_value(value = nil) ⇒ Hash, View
Set the maximum value to search.
334 335 336 |
# File 'lib/mongo/collection/view/readable.rb', line 334 def max_value(value = nil) configure(:max_value, value) end |
#min_value(value = nil) ⇒ Hash, View
Set the minimum value to search.
348 349 350 |
# File 'lib/mongo/collection/view/readable.rb', line 348 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.
488 489 490 491 |
# File 'lib/mongo/collection/view/readable.rb', line 488 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.
361 362 363 |
# File 'lib/mongo/collection/view/readable.rb', line 361 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.
379 380 381 382 |
# File 'lib/mongo/collection/view/readable.rb', line 379 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.
395 396 397 398 |
# File 'lib/mongo/collection/view/readable.rb', line 395 def read(value = nil) return read_preference if value.nil? configure(:read, value) end |
#return_key(value = nil) ⇒ true, ...
Set whether to return only the indexed field or fields.
410 411 412 |
# File 'lib/mongo/collection/view/readable.rb', line 410 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.
425 426 427 |
# File 'lib/mongo/collection/view/readable.rb', line 425 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.
441 442 443 |
# File 'lib/mongo/collection/view/readable.rb', line 441 def skip(number = nil) configure(:skip, number) end |
#snapshot(value = nil) ⇒ Object
This option is deprecated as of MongoDB server version 4.0.
When set to true, prevents documents from returning more than once.
Set the snapshot value for the view.
459 460 461 |
# File 'lib/mongo/collection/view/readable.rb', line 459 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.
474 475 476 |
# File 'lib/mongo/collection/view/readable.rb', line 474 def sort(spec = nil) configure(:sort, spec) end |