class Mongo::Protocol::Query::Upconverter
Converts legacy query messages to the appropriare OP_COMMAND style message.
@since 2.1.0
Constants
- 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
@return [ String ] collection The name of the collection.
@return [ BSON::Document, Hash ] filter The query filter or command.
@return [ Array<Symbol> ] flags The flags.
@return [ BSON::Document, Hash ] options The options.
Public Class Methods
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 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
Public Instance Methods
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 307 def command command? ? op_command : find_command end
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 320 def command_name ((filter[:$query] || !command?) ? :find : filter.keys.first).to_s end
Private Instance Methods
# File lib/mongo/protocol/query.rb, line 326 def command? collection == Database::COMMAND end
# 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
# 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
# File lib/mongo/protocol/query.rb, line 330 def query_filter filter[:$query] || filter end