module Mongo::Operation::Find::Builder::Legacy

Builds a legacy OP_QUERY specification from options.

@api private

Constants

DRIVER_MAPPINGS

Mappings from driver options to legacy server values.

@since 2.2.0

Public Instance Methods

query_options(spec, connection) click to toggle source
# File lib/mongo/operation/find/builder/legacy.rb, line 88
                def query_options(spec, connection)
  query_options = {
    project: spec[:projection],
    skip: spec[:skip],
    limit: spec[:limit],
    # batch_size is converted to batchSize by Mongo::Protocol::Query.
    batch_size: spec[:batch_size],
  }

  unless (flags = Builder::Flags.map_flags(spec)).empty?
    query_options[:flags] = ((query_options[:flags] || []) + flags).uniq
  end

  query_options
end
read_pref_formatted(spec) click to toggle source
# File lib/mongo/operation/find/builder/legacy.rb, line 106
                def read_pref_formatted(spec)
  if spec[:read_preference]
    raise ArgumentError, "Spec cannot include :read_preference here, use :read"
  end

  if read = spec[:read]
    read_pref = ServerSelector.get(read).to_mongos
    Mongo::Lint.validate_camel_case_read_preference(read_pref)
    read_pref
  else
    nil
  end
end
selector(spec, connection) click to toggle source
# File lib/mongo/operation/find/builder/legacy.rb, line 45
                def selector(spec, connection)
  if Lint.enabled?
    if spec.keys.any? { |k| String === k }
      raise Error::LintError, "The spec must contain symbol keys only"
    end
  end

  # Server versions that do not have the find command feature
  # (versions older than 3.2) do not support the allow_disk_use option
  # but perform no validation and will not raise an error if it is
  # specified. If the allow_disk_use option is specified, raise an error
  # to alert the user.
  unless spec[:allow_disk_use].nil?
    raise Error::UnsupportedOption.allow_disk_use_error
  end

  if spec[:collation] && !connection.features.collation_enabled?
    raise Error::UnsupportedCollation
  end

  modifiers = {}
  DRIVER_MAPPINGS.each do |k, server_k|
    unless (value = spec[k]).nil?
      modifiers[server_k] = value
    end
  end

  selector = spec[:filter] || BSON::Document.new
  # Write nil into rp if not talking to mongos, rather than false
  rp = if connection.description.mongos?
    read_pref_formatted(spec)
  end
  if modifiers.any? || rp
    selector = {'$query' => selector}.update(modifiers)

    if rp
      selector['$readPreference'] = rp
    end
  end

  selector
end