lib/mongo/operation/shared/sessions_supported.rb
def add_read_preference(sel, connection)
        Lint.assert_type(connection, Server::Connection)

        # https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#topology-type-single
        read_doc = if connection.description.standalone?
          # Read preference is never sent to standalones.
          nil
        elsif connection.server.load_balancer?
          read&.to_mongos
        elsif connection.description.mongos?
          # When server is a mongos:
          # - $readPreference is never sent when mode is 'primary'
          # - Otherwise $readPreference is sent
          # When mode is 'secondaryPreferred' $readPreference is currently
          # required to only be sent when a non-mode field (i.e. tag_sets)
          # is present, but this causes wrong behavior (DRIVERS-1642).
          read&.to_mongos
        elsif connection.server.cluster.single?
          # In Single topology:
          # - If no read preference is specified by the application, the driver
          #   adds mode: primaryPreferred.
          # - If a read preference is specified by the application, the driver
          #   replaces the mode with primaryPreferred.
          read_doc = if read
            BSON::Document.new(read.to_doc)
          else
            BSON::Document.new
          end
          if [nil, 'primary'].include?(read_doc['mode'])
            read_doc['mode'] = 'primaryPreferred'
          end
          read_doc
        else
          # In replica sets, read preference is passed to the server if one
          # is specified by the application, and there is no default.
          read&.to_doc
        end

        if read_doc
          sel['$readPreference'] = read_doc
        end
      end
# Adds $readPreference field to the command document.
#
# $readPreference is only sent when the server is a mongos,
# following the rules described in
# https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#passing-read-preference-to-mongos.
# The topology does not matter for figuring out whether to send
# $readPreference since the decision is always made based on
# server type.
#
# $readPreference is sent to OP_MSG-grokking replica set members.
#
# @param [ Hash ] sel Existing command document which will be mutated.
# @param [ Server::Connection ] connection The connection that the
#   operation will be executed on.
Adds $readPreference field to the command document.

$readPreference is sent to OP_MSG-grokking replica set members.
Adds $readPreference field to the command document . $readPreference is sent to OP_MSG - grokking replica set members .
def add_read_preference(sel, connection)
        Lint.assert_type(connection, Server::Connection)
        read_doc = if connection.description.standalone?
          nil
        elsif connection.server.load_balancer?
          read&.to_mongos
        elsif connection.description.mongos?
          read&.to_mongos
        elsif connection.server.cluster.single?
          read_doc = if read
            BSON::Document.new(read.to_doc)
          else
            BSON::Document.new
          end
          if [nil, 'primary'].include?(read_doc['mode'])
            read_doc['mode'] = 'primaryPreferred'
          end
          read_doc
        else
          read&.to_doc
        end
        if read_doc
          sel['$readPreference'] = read_doc
        end
      end
def add_read_preference ( sel , connection ) Lint . assert_type ( connection , Server :: Connection ) read_doc = if connection . description . standalone? nil elsif connection . server . load_balancer? read &. to_mongos elsif connection . description . mongos? read &. to_mongos elsif connection . server . cluster . single? read_doc = if read BSON :: Document . new ( read . to_doc ) else BSON :: Document . new end if [ nil , 'primary' ] . include? ( read_doc [ 'mode' ] ) read_doc [ 'mode' ] = 'primaryPreferred' end read_doc else read &. to_doc end if read_doc sel [ '$readPreference' ] = read_doc end end
Adds $readPreference field to the command document.
Adds $readPreference field to the command document .
# https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#topology-type-single # Read preference is never sent to standalones. # When server is a mongos: # - $readPreference is never sent when mode is 'primary' # - Otherwise $readPreference is sent # When mode is 'secondaryPreferred' $readPreference is currently # required to only be sent when a non-mode field (i.e. tag_sets) # is present, but this causes wrong behavior (DRIVERS-1642). # In Single topology: # - If no read preference is specified by the application, the driver #   adds mode: primaryPreferred. # - If a read preference is specified by the application, the driver #   replaces the mode with primaryPreferred. # In replica sets, read preference is passed to the server if one # is specified by the application, and there is no default.
sel
connection
connection The connection that the operation will be executed on.
connection The connection that the operation will be executed on .