Class: Mongo::Protocol::Query::Upconverter

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/protocol/query.rb

Overview

Converts legacy query messages to the appropriare OP_COMMAND style message.

Since:

  • 2.1.0

Constant Summary collapse

OPTION_MAPPINGS =

Mappings of the options to the find command options.

Since:

  • 2.1.0

{
  :project => 'projection',
  :skip => 'skip',
  :limit => 'limit',
  :batch_size => 'batchSize'
}.freeze
SPECIAL_FIELD_MAPPINGS =

Since:

  • 2.1.0

{
  :$readPreference => '$readPreference',
  :$orderby => 'sort',
  :$hint => 'hint',
  :$comment => 'comment',
  :$returnKey => 'returnKey',
  :$snapshot => 'snapshot',
  :$maxScan => 'maxScan',
  :$max => 'max',
  :$min => 'min',
  :$maxTimeMS => 'maxTimeMS',
  :$showDiskLoc => 'showRecordId',
  :$explain => 'explain'
}.freeze
FLAG_MAPPINGS =

Mapping of flags to find command options.

Since:

  • 2.1.0

{
  :tailable_cursor => 'tailable',
  :oplog_replay => 'oplogReplay',
  :no_cursor_timeout => 'noCursorTimeout',
  :await_data => 'awaitData',
  :partial => 'allowPartialResults'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, filter, options, flags) ⇒ Upconverter

Instantiate the upconverter.

Examples:

Instantiate the upconverter.

Upconverter.new('users', { name: 'test' }, { skip: 10 })

Parameters:

  • collection (String)

    The name of the collection.

  • filter (BSON::Document, Hash)

    The filter or command.

  • options (BSON::Document, Hash)

    The options.

  • flags (Array<Symbol>)

    The flags.

Since:

  • 2.1.0



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mongo/protocol/query.rb', line 283

def initialize(collection, filter, options, flags)
  # Although the docstring claims both hashes and BSON::Documents
  # are acceptable, this class expects the filter and options to
  # contain symbol keys which isn't what the operation layer produces.
  unless BSON::Document === filter
    raise ArgumentError, 'Filter must provide indifferent access'
  end
  unless BSON::Document === options
    raise ArgumentError, 'Options must provide indifferent access'
  end
  @collection = collection
  @filter = filter
  @options = options
  @flags = flags
end

Instance Attribute Details

#collectionString (readonly)

Returns collection The name of the collection.

Returns:

  • (String)

    collection The name of the collection.

Since:

  • 2.1.0



261
262
263
# File 'lib/mongo/protocol/query.rb', line 261

def collection
  @collection
end

#filterBSON::Document, Hash (readonly)

Returns filter The query filter or command.

Returns:

  • (BSON::Document, Hash)

    filter The query filter or command.

Since:

  • 2.1.0



264
265
266
# File 'lib/mongo/protocol/query.rb', line 264

def filter
  @filter
end

#flagsArray<Symbol> (readonly)

Returns flags The flags.

Returns:

  • (Array<Symbol>)

    flags The flags.

Since:

  • 2.1.0



270
271
272
# File 'lib/mongo/protocol/query.rb', line 270

def flags
  @flags
end

#optionsBSON::Document, Hash (readonly)

Returns options The options.

Returns:

  • (BSON::Document, Hash)

    options The options.

Since:

  • 2.1.0



267
268
269
# File 'lib/mongo/protocol/query.rb', line 267

def options
  @options
end

Instance Method Details

#commandBSON::Document

Get the upconverted command.

Examples:

Get the command.

upconverter.command

Returns:

  • (BSON::Document)

    The upconverted command.

Since:

  • 2.1.0



307
308
309
# File 'lib/mongo/protocol/query.rb', line 307

def command
  command? ? op_command : find_command
end

#command_nameString

Get the name of the command. If the collection is $cmd then it’s the first key in the filter, otherwise it’s a find.

Examples:

Get the command name.

upconverter.command_name

Returns:

  • (String)

    The command name.

Since:

  • 2.1.0



320
321
322
# File 'lib/mongo/protocol/query.rb', line 320

def command_name
  ((filter[:$query] || !command?) ? :find : filter.keys.first).to_s
end