Skip to content

MongoDB\Database set ReadPreference not work #322

Closed
@keita1314

Description

@keita1314

code as this:

>$db = $db->withOptions(array('readPreference' =>new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY)));
        var_dump($db);
        $query = array(
            "hash" => $this->hash(),
            "time" => array(
                '$gte' => $start,
                '$lt' => $end
            )
        );
        $option = array(
            "projection" => array(
                "_id" => 0,
                "hash" => 0
            ),
            "sort" => array(
                "time" => 1
            )
        );
        $project = $this->project->hash();
        return $db->{$project}->find($query, $option);

$db set readpreference as secondary via withOptions , and $db var_dump like below:

>object(MongoDB\Database)#16 (6) {
  ["databaseName":"MongoDB\Database":private]=>
  string(9) "xxx"
  ["manager":"MongoDB\Database":private]=>
  object(MongoDB\Driver\Manager)#5 (2) {
    ["uri"]=>
    string(37) "mongodb://%2Ftmp%2Fmongodb-30000.sock"
    ["cluster"]=>
    array(1) {
      [0]=>
      array(10) {
        ["host"]=>
        string(23) "/tmp/mongodb-30000.sock"
        ["port"]=>
        int(27017)
        ["type"]=>
        int(2)
        ["is_primary"]=>
        bool(false)
        ["is_secondary"]=>
        bool(false)
        ["is_arbiter"]=>
        bool(false)
        ["is_hidden"]=>
        bool(false)
        ["is_passive"]=>
        bool(false)
        ["last_is_master"]=>
        array(9) {
          ["ismaster"]=>
          bool(true)
          ["msg"]=>
          string(8) "isdbgrid"
          ["maxBsonObjectSize"]=>
          int(16777216)
          ["maxMessageSizeBytes"]=>
          int(48000000)
          ["maxWriteBatchSize"]=>
          int(1000)
          ["localTime"]=>
          object(MongoDB\BSON\UTCDateTime)#19 (1) {
            ["milliseconds"]=>
            string(13) "1484032841355"
          }
          ["maxWireVersion"]=>
          int(3)
          ["minWireVersion"]=>
          int(0)
          ["ok"]=>
          float(1)
        }
        ["round_trip_time"]=>
        int(2)
      }
    }
  }
  ["readConcern":"MongoDB\Database":private]=>
  object(MongoDB\Driver\ReadConcern)#8 (0) {
  }
  ["readPreference":"MongoDB\Database":private]=>
  object(MongoDB\Driver\ReadPreference)#15 (1) {
    ["mode"]=>
    string(9) "secondary"
  }
  ["typeMap":"MongoDB\Database":private]=>
  array(3) {
    ["root"]=>
    string(5) "array"
    ["document"]=>
    string(5) "array"
    ["array"]=>
    string(5) "array"
  }
  ["writeConcern":"MongoDB\Database":private]=>
  object(MongoDB\Driver\WriteConcern)#10 (0) {
  }
}

driver version is 1.2.1, but all query still on master, $collection->withOptions, find($filter, $option) is also not work but client's uriOptions array("readPreference" => "secondary") is ok, why?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions