class Mongo::Protocol::Query::Upconverter

Converts legacy query messages to the appropriare OP_COMMAND style message.

@since 2.1.0

Constants

FILTER

Filter attribute constant.

@since 2.1.0

FIND

Find command constant.

@since 2.1.0

FLAG_MAPPINGS

Mapping of flags to find command options.

@since 2.1.0

OPTION_MAPPINGS

Mappings of the options to the find command options.

@since 2.1.0

SPECIAL_FIELD_MAPPINGS

Attributes

collection[R]

@return [ String ] collection The name of the collection.

filter[R]

@return [ BSON::Document, Hash ] filter The query filter or command.

flags[R]

@return [ Array<Symbol> ] flags The flags.

options[R]

@return [ BSON::Document, Hash ] options The options.

Public Class Methods

new(collection, filter, options, flags) click to toggle source

Instantiate the upconverter.

@example Instantiate the upconverter.

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

@param [ String ] collection The name of the collection. @param [ BSON::Document, Hash ] filter The filter or command. @param [ BSON::Document, Hash ] options The options. @param [ Array<Symbol> ] flags The flags.

@since 2.1.0

# File lib/mongo/protocol/query.rb, line 247
def initialize(collection, filter, options, flags)
  @collection = collection
  @filter = filter
  @options = options
  @flags = flags
end

Public Instance Methods

command() click to toggle source

Get the upconverted command.

@example Get the command.

upconverter.command

@return [ BSON::Document ] The upconverted command.

@since 2.1.0

# File lib/mongo/protocol/query.rb, line 262
def command
  command? ? op_command : find_command
end
command_name() click to toggle source

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.

@example Get the command name.

upconverter.command_name

@return [ String ] The command name.

@since 2.1.0

# File lib/mongo/protocol/query.rb, line 275
def command_name
  ((filter[:$query] || !command?) ? FIND : filter.keys.first).to_s
end

Private Instance Methods

command?() click to toggle source
# File lib/mongo/protocol/query.rb, line 281
def command?
  collection == Database::COMMAND
end
find_command() click to toggle source
# File lib/mongo/protocol/query.rb, line 297
def find_command
  document = BSON::Document.new
  document.store(FIND, collection)
  document.store(FILTER, query_filter)
  OPTION_MAPPINGS.each do |legacy, option|
    document.store(option, options[legacy]) unless options[legacy].nil?
  end
  Mongo::Lint.validate_camel_case_read_preference(filter['readPreference'])
  SPECIAL_FIELD_MAPPINGS.each do |special, normal|
    document.store(normal, filter[special]) unless filter[special].nil?
  end
  FLAG_MAPPINGS.each do |legacy, flag|
    document.store(flag, true) if flags.include?(legacy)
  end
  document
end
op_command() click to toggle source
# File lib/mongo/protocol/query.rb, line 289
def op_command
  document = BSON::Document.new
  query_filter.each do |field, value|
    document.store(field.to_s, value)
  end
  document
end
query_filter() click to toggle source
# File lib/mongo/protocol/query.rb, line 285
def query_filter
  filter[:$query] || filter
end