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.

Since:

  • 2.0.0

Constant Summary

QUERY =

The query modifier constant.

Since:

  • 2.2.0

'$query'.freeze
MODIFIERS =

The modifiers option constant.

Since:

  • 2.2.0

'modifiers'.freeze

Instance Method Summary collapse

Instance Method Details

#aggregate(pipeline, options = {}) ⇒ Aggregation

Execute an aggregation on the collection view.

Examples:

Aggregate documents.

view.aggregate([
  { "$group" => { "_id" => "$city", "tpop" => { "$sum" => "$pop" }}}
])

Parameters:

  • pipeline (Array<Hash>)

    The aggregation pipeline.

  • options (Hash) (defaults to: {})

    The aggregation options.

Returns:

Since:

  • 2.0.0



47
48
49
# File 'lib/mongo/collection/view/readable.rb', line 47

def aggregate(pipeline, options = {})
  Aggregation.new(self, pipeline, options)
end

#allow_partial_resultsView

Allows the query to get partial results if some shards are down.

Examples:

Allow partial results.

view.allow_partial_results

Returns:

  • (View)

    The new view.

Since:

  • 2.0.0



59
60
61
# File 'lib/mongo/collection/view/readable.rb', line 59

def allow_partial_results
  configure(:allow_partial_results, true)
end

#await_dataView

Tell the query's cursor to stay open and wait for data.

Examples:

Await data on the cursor.

view.await_data

Returns:

  • (View)

    The new view.

Since:

  • 2.0.0



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

Note:

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.

Examples:

Set the batch size.

view.batch_size(5)

Parameters:

  • batch_size (Integer) (defaults to: nil)

    The size of each batch of results.

Returns:

  • (Integer, View)

    Either the batch_size value or a

Since:

  • 2.0.0



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

Note:

Set profilingLevel to 2 and the comment will be logged in the profile collection along with the query.

Associate a comment with the query.

Examples:

Add a comment.

view.comment('slow query')

Parameters:

  • comment (String) (defaults to: nil)

    The comment to be associated with the query.

Returns:

  • (String, View)

    Either the comment or a new View.

Since:

  • 2.0.0



106
107
108
# File 'lib/mongo/collection/view/readable.rb', line 106

def comment(comment = nil)
  configure(:comment, comment)
end

#count(opts = {}) ⇒ Integer

Get a count of matching documents in the collection.

Examples:

Get the number of documents in the collection.

collection_view.count

Parameters:

  • opts (Hash) (defaults to: {})

    Options for the count command.

Options Hash (opts):

  • :skip (Integer)

    The number of documents to skip.

  • :hint (Hash)

    Override default index selection and force MongoDB to use a specific index for the query.

  • :limit (Integer)

    Max number of docs to return.

  • :max_time_ms (Integer)

    The maximum amount of time to allow the command to run.

  • :read (Hash)

    The read preference options.

  • :collation (Hash)

    The collation to use.

Returns:

  • (Integer)

    The document count.

Since:

  • 2.0.0



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mongo/collection/view/readable.rb', line 129

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
  preference = ServerSelector.get(opts[:read] || read)
  read_with_retry do
    server = preference.select_server(cluster, false)
    apply_collation!(cmd, server, opts)
    Operation::Commands::Command.new({
                                       :selector => cmd,
                                       :db_name => database.name,
                                       :options => { :limit => -1 },
                                       :read => preference,
                                     }).execute(server).n.to_i

  end
end

#cursor_type(type = nil) ⇒ :tailable, ...

The type of cursor to use. Can be :tailable or :tailable_await.

Examples:

Set the cursor type.

view.cursor_type(:tailable)

Parameters:

  • type (:tailable, :tailable_await) (defaults to: nil)

    The cursor type.

Returns:

  • (:tailable, :tailable_await, View)

    Either the cursor type setting or a new View.

Since:

  • 2.3.0



450
451
452
# File 'lib/mongo/collection/view/readable.rb', line 450

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.

Examples:

Get the distinct values.

collection_view.distinct('name')

Parameters:

  • field_name (String, Symbol)

    The name of the field.

  • opts (Hash) (defaults to: {})

    Options for the distinct command.

Options Hash (opts):

  • :max_time_ms (Integer)

    The maximum amount of time to allow the command to run.

  • :read (Hash)

    The read preference options.

  • :collation (Hash)

    The collation to use.

Returns:

  • (Array<Object>)

    The list of distinct values.

Since:

  • 2.0.0



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mongo/collection/view/readable.rb', line 166

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
  preference = ServerSelector.get(opts[:read] || read)
  read_with_retry do
    server = preference.select_server(cluster, false)
    apply_collation!(cmd, server, opts)
    Operation::Commands::Command.new({
                                       :selector => cmd,
                                       :db_name => database.name,
                                       :options => { :limit => -1 },
                                       :read => preference
                                     }).execute(server).first['values']

  end
end

#hint(hint = nil) ⇒ Hash, View

The index that MongoDB will be forced to use for the query.

Examples:

Set the index hint.

view.hint(name: 1)

Parameters:

  • hint (Hash) (defaults to: nil)

    The index to use for the query.

Returns:

  • (Hash, View)

    Either the hint or a new View.

Since:

  • 2.0.0



196
197
198
# File 'lib/mongo/collection/view/readable.rb', line 196

def hint(hint = nil)
  configure(:hint, hint)
end

#limit(limit = nil) ⇒ Integer, View

The max number of docs to return from the query.

Examples:

Set the limit.

view.limit(5)

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of docs to return.

Returns:

  • (Integer, View)

    Either the limit or a new View.

Since:

  • 2.0.0



210
211
212
# File 'lib/mongo/collection/view/readable.rb', line 210

def limit(limit = nil)
  configure(:limit, limit)
end

#map_reduce(map, reduce, options = {}) ⇒ MapReduce

Execute a map/reduce operation on the collection view.

Examples:

Execute a map/reduce.

view.map_reduce(map, reduce)

Parameters:

  • map (String)

    The map js function.

  • reduce (String)

    The reduce js function.

  • options (Hash) (defaults to: {})

    The map/reduce options.

Returns:

Since:

  • 2.0.0



226
227
228
# File 'lib/mongo/collection/view/readable.rb', line 226

def map_reduce(map, reduce, options = {})
  MapReduce.new(self, map, reduce, options)
end

#max_await_time_ms(max = nil) ⇒ Integer, View

A cumulative time limit in milliseconds for processing get more operations on a cursor.

Examples:

Set the max await time ms value.

view.max_await_time_ms(500)

Parameters:

  • max (Integer) (defaults to: nil)

    The max time in milliseconds.

Returns:

  • (Integer, View)

    Either the max await time ms value or a new View.

Since:

  • 2.1.0



422
423
424
# File 'lib/mongo/collection/view/readable.rb', line 422

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.

Examples:

Set the max scan value.

view.max_scan(1000)

Parameters:

  • value (Integer) (defaults to: nil)

    The max number to scan.

Returns:

  • (Integer, View)

    The value or a new View.

Since:

  • 2.0.0



240
241
242
# File 'lib/mongo/collection/view/readable.rb', line 240

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.

Examples:

Set the max time ms value.

view.max_time_ms(500)

Parameters:

  • max (Integer) (defaults to: nil)

    The max time in milliseconds.

Returns:

  • (Integer, View)

    Either the max time ms value or a new View.

Since:

  • 2.1.0



436
437
438
# File 'lib/mongo/collection/view/readable.rb', line 436

def max_time_ms(max = nil)
  configure(:max_time_ms, max)
end

#max_value(value = nil) ⇒ Hash, View

Set the maximum value to search.

Examples:

Set the max value.

view.max_value(_id: 1)

Parameters:

  • value (Hash) (defaults to: nil)

    The max field and value.

Returns:

  • (Hash, View)

    The value or a new View.

Since:

  • 2.1.0



254
255
256
# File 'lib/mongo/collection/view/readable.rb', line 254

def max_value(value = nil)
  configure(:max_value, value)
end

#min_value(value = nil) ⇒ Hash, View

Set the minimum value to search.

Examples:

Set the min value.

view.min_value(_id: 1)

Parameters:

  • value (Hash) (defaults to: nil)

    The min field and value.

Returns:

  • (Hash, View)

    The value or a new View.

Since:

  • 2.1.0



268
269
270
# File 'lib/mongo/collection/view/readable.rb', line 268

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.

Examples:

Set the modifiers document.

view.modifiers(:$orderby => Mongo::Index::ASCENDING)

Parameters:

  • doc (Hash) (defaults to: nil)

    The modifiers document.

Returns:

  • (Hash, View)

    Either the modifiers document or a new View.

Since:

  • 2.1.0



406
407
408
409
# File 'lib/mongo/collection/view/readable.rb', line 406

def modifiers(doc = nil)
  return Builder::Modifiers.map_server_modifiers(options) if doc.nil?
  new(options.merge(Builder::Modifiers.map_driver_options(doc)))
end

#no_cursor_timeoutView

The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that.

Examples:

Set the cursor to not timeout.

view.no_cursor_timeout

Returns:

  • (View)

    The new view.

Since:

  • 2.0.0



281
282
283
# File 'lib/mongo/collection/view/readable.rb', line 281

def no_cursor_timeout
  configure(:no_cursor_timeout, true)
end

#projection(document = nil) ⇒ Hash, View

Note:

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.

Examples:

Set the fields to include or exclude.

view.projection(name: 1)

Parameters:

  • document (Hash) (defaults to: nil)

    The field and 1 or 0, to include or exclude it.

Returns:

  • (Hash, View)

    Either the fields or a new View.

Since:

  • 2.0.0



299
300
301
302
# File 'lib/mongo/collection/view/readable.rb', line 299

def projection(document = nil)
  validate_doc!(document) if document
  configure(:projection, document)
end

#read(value = nil) ⇒ Symbol, View

Note:

If none is specified for the query, the read preference of the collection will be used.

The read preference to use for the query.

Parameters:

  • value (Hash) (defaults to: nil)

    The read preference mode to use for the query.

Returns:

  • (Symbol, View)

    Either the read preference or a new View.

Since:

  • 2.0.0



315
316
317
318
319
# File 'lib/mongo/collection/view/readable.rb', line 315

def read(value = nil)
  return default_read if value.nil?
  selector = ServerSelector.get(value)
  configure(:read, selector)
end

#return_key(value = nil) ⇒ true, ...

Set whether to return only the indexed field or fields.

Examples:

Set the return key value.

view.return_key(true)

Parameters:

  • value (true, false) (defaults to: nil)

    The return key value.

Returns:

  • (true, false, View)

    The value or a new View.

Since:

  • 2.1.0



331
332
333
# File 'lib/mongo/collection/view/readable.rb', line 331

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.

Examples:

Set show disk location option.

view.show_disk_loc(true)

Parameters:

  • value (true, false) (defaults to: nil)

    The value for the field.

Returns:

  • (true, false, View)

    Either the value or a new View.

Since:

  • 2.0.0



346
347
348
# File 'lib/mongo/collection/view/readable.rb', line 346

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.

Examples:

Set the number to skip.

view.skip(10)

Parameters:

  • number (Integer) (defaults to: nil)

    Number of docs to skip.

Returns:

  • (Integer, View)

    Either the skip value or a new View.

Since:

  • 2.0.0



362
363
364
# File 'lib/mongo/collection/view/readable.rb', line 362

def skip(number = nil)
  configure(:skip, number)
end

#snapshot(value = nil) ⇒ Object

Note:

When set to true, prevents documents from returning more than once.

Set the snapshot value for the view.

Examples:

Set the snapshot value.

view.snapshot(true)

Parameters:

  • value (true, false) (defaults to: nil)

    The snapshot value.

Since:

  • 2.0.0



377
378
379
# File 'lib/mongo/collection/view/readable.rb', line 377

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.

Examples:

Set the sort criteria

view.sort(name: -1)

Parameters:

  • spec (Hash) (defaults to: nil)

    The attributes and directions to sort by.

Returns:

  • (Hash, View)

    Either the sort setting or a new View.

Since:

  • 2.0.0



392
393
394
# File 'lib/mongo/collection/view/readable.rb', line 392

def sort(spec = nil)
  configure(:sort, spec)
end