123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Inherits: Object
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

  • FLAG_MAPPINGS =

    Mapping of flags to find command options.

    Since:

    • 2.1.0

    # File 'lib/mongo/protocol/query.rb', line 252
    {
      :tailable_cursor => 'tailable',
      :oplog_replay => 'oplogReplay',
      :no_cursor_timeout => 'noCursorTimeout',
      :await_data => 'awaitData',
      :partial => 'allowPartialResults'
    }.freeze
  • OPTION_MAPPINGS =

    Mappings of the options to the find command options.

    Since:

    • 2.1.0

    # File 'lib/mongo/protocol/query.rb', line 227
    {
      :project => 'projection',
      :skip => 'skip',
      :limit => 'limit',
      :batch_size => 'batchSize'
    }.freeze
  • SPECIAL_FIELD_MAPPINGS =

    Since:

    • 2.1.0

    # File 'lib/mongo/protocol/query.rb', line 234
    {
      :$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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(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

[ GitHub ]

  
# 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:

  • (String)

    collection The name of the collection.

Since:

  • 2.1.0

[ GitHub ]

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

attr_reader :collection

#commandBSON::Document (readonly)

Get the upconverted command.

Examples:

Get the command.

upconverter.command

Returns:

  • (BSON::Document)

    The upconverted command.

Since:

  • 2.1.0

[ GitHub ]

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

def command
  command? ? op_command : find_command
end

#command?Boolean (readonly, private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/query.rb', line 326

def command?
  collection == Database::COMMAND
end

#filterBSON::Document, Hash (readonly)

Returns:

  • (BSON::Document, Hash)

    filter The query filter or command.

Since:

  • 2.1.0

[ GitHub ]

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

attr_reader :filter

#flagsArray<Symbol> (readonly)

Returns:

  • (Array<Symbol>)

    flags The flags.

Since:

  • 2.1.0

[ GitHub ]

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

attr_reader :flags

#optionsBSON::Document, Hash (readonly)

Returns:

  • (BSON::Document, Hash)

    options The options.

Since:

  • 2.1.0

[ GitHub ]

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

attr_reader :options

Instance Method Details

#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

[ GitHub ]

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

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

#find_command (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/query.rb', line 342

def find_command
  document = BSON::Document.new(
    find: collection,
    filter: query_filter,
  )
  OPTION_MAPPINGS.each do |legacy, option|
    document.store(option, options[legacy]) unless options[legacy].nil?
  end
  if Lint.enabled?
    filter.each do |k, v|
      unless String === k
        raise Error::LintError, "All keys in filter must be strings: #{filter.inspect}"
      end
    end
  end
  Lint.validate_camel_case_read_preference(filter['readPreference'])
  SPECIAL_FIELD_MAPPINGS.each do |special, normal|
    unless (v = filter[special]).nil?
      document.store(normal, v)
    end
  end
  FLAG_MAPPINGS.each do |legacy, flag|
    document.store(flag, true) if flags.include?(legacy)
  end
  document
end

#op_command (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/query.rb', line 334

def op_command
  document = BSON::Document.new
  query_filter.each do |field, value|
    document.store(field.to_s, value)
  end
  document
end

#query_filter (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/query.rb', line 330

def query_filter
  filter[:$query] || filter
end